You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Colab中导入自定义模块?非Pip发布方案咨询

解决方案:在Google Colab中导入自定义Python模块(无需发布到PyPI)

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:

  1. Upload your .py files (or entire module folder) to a dedicated Drive folder, e.g., /MyDrive/Colab_Course_Utils/
  2. 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')
  1. 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

MethodProsConsBest For
GitHub Pip InstallAuto-updates, minimal student setupRequires Git repo, private repos need PATPublic/team-managed modules
Session File UploadNo setup required, quick for testingTemporary, requires re-upload each timeOne-off tests with single files
Google Drive MountPermanent, easy updates, clean notebooksRequires one-time Drive authenticationLong-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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:01:11