简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM