如何在Google Colab中导入自定义模块?非Pip发布方案咨询
Great question! I’ve helped several instructors set up custom modules for Colab-based courses, so let’s break down your options clearly—no PyPI required, and we’ll cover the GitHub pip install support too, plus the best solution for your teaching needs:
1. Yes, Colab supports installing modules from GitHub via Pip
You don’t need to publish to PyPI to install your custom code from GitHub. For public repositories, just run this command in a Colab code cell:
!pip install git+https://github.com/your-username/your-repo-name.git
If your repo is private, generate a GitHub Personal Access Token (PAT) with repo access, then use this format to authenticate:
!pip install git+https://your-pat-token-here@github.com/your-username/your-repo-name.git
This works seamlessly in Colab, and it pulls the latest version of your code each time. It’s great if you’re actively updating your methods and want students to get the newest changes automatically.
2. Import via direct file upload (temporary solution)
If you only have a small number of .py files, you can upload them directly to your Colab session:
- Click the Files icon in the left sidebar, then select Upload to session storage
- Once uploaded, you can import the module just like any local file:
import your_module
The catch? Files stored in the session are deleted when you close the notebook. This is fine for quick tests, but not ideal for repeated use in a course—students would have to re-upload every time they work on the notebook.
3. Mount Google Drive (the optimal solution for teaching)
Storing your custom modules in Google Drive and mounting it in Colab is hands-down the best approach for your use case. Here’s how to set it up:
- Upload your
.pyfiles (or entire module folder) to a dedicated Drive folder, e.g.,/MyDrive/Colab_Course_Utils/ - In Colab, run this code to mount your Drive (you’ll be prompted to authenticate with your Google account):
from google.colab import drive drive.mount('/content/drive')
- Add the Drive folder path to Python’s system path so it can find your modules:
import sys sys.path.append('/content/drive/MyDrive/Colab_Course_Utils/') # Now import your module like normal import ds_basics
Why this is perfect for your course:
- Permanent storage: Files stay in Drive, so students don’t have to re-upload or re-install every session
- Easy updates: You can edit the module files directly in Drive, and students will get the latest version the next time they run the import code
- Clean notebooks: Your students only see the import lines, not all the underlying code—they can focus on the core data science content you’re teaching
Quick Comparison of Options
| Method | Pros | Cons | Best For |
|---|---|---|---|
| GitHub Pip Install | Auto-updates, minimal student setup | Requires Git repo, private repos need PAT | Public/team-managed modules |
| Session File Upload | No setup required, quick for testing | Temporary, requires re-upload each time | One-off tests with single files |
| Google Drive Mount | Permanent, easy updates, clean notebooks | Requires one-time Drive authentication | Long-term courses, custom module management |
As you can see, mounting Google Drive is the optimal choice for your goal of keeping notebooks focused on core content while giving students access to your custom methods.
内容的提问来源于stack exchange,提问作者Pablo Marin-Garcia

