Jean-Pat
Jean-Pat

Reputation: 1871

no module named ... try to importcustom python module from a notebook in google Colab

In a google drive, suppose a jupyter notebook and a python file my_module.py copied in the same directory. How to import my_module from the notebook when run with google colaboratory?

When the notebook is run locally, import my_module just works.

Upvotes: 0

Views: 2070

Answers (2)

Mohamed Jihad
Mohamed Jihad

Reputation: 466

I has same problem before i fixed it. there is few way.

1.Use right python version, google colaboratory have 2 python version 2.7 and 3.6.1.

2.Add init.py blank content in directory, example:

dir/
__init__.py
my_module.py
yournotebook.ipynb

3.Remove all pycache *.pyc in dir/

cd dir
!rm *pyc

Upvotes: 0

Ami F
Ami F

Reputation: 2272

At the bottom of the welcome notebook at colab.research.google.com there's a link to an example notebook titled "Loading and saving data: local files, Drive, Sheets, Google Cloud Storage" (https://colab.research.google.com/notebook#fileId=/v2/external/notebooks/io.ipynb). This gives recipes for how to copy files from Google Drive to the colaboratory runtime which you can use to copy your module code so it will be visible to the runtime's import machinery.

Upvotes: 1

Related Questions