Importing a python module from the python script from drive in colab

浪子不回头ぞ 提交于 2019-12-24 11:34:32

问题


I am currently working on a project on Machine Learning on Google Colab which uses Tensorflow API. I created a folder and uploaded it on google drive to run on google Colab. I successfully mounted the google drive and can run the script, But when I try importing another module from the script present in the same folder, it throws an error

    from . import inference
    ImportError: cannot import name 'inference'

I tried finding out a solution for this but found results stating how to import a module directly to the colab notebook.

Please tell me what I am missing here.

Edit: The folder structure is

-nmt
  -nmt.py
  -train.py
  -inference.py
  -utils
    -evaluation.py

and so on. And I am running the python file from the nmt folder. I am getting relative import errors.


回答1:


Try this. First, upload the .py file and then save it locally. Then you may use it as you like it.

from google.colab import files
src = list(files.upload().values())[0]
open('library_you_want_to_use.py','wb').write(src)
import library_you_want_to_use


来源:https://stackoverflow.com/questions/52659755/importing-a-python-module-from-the-python-script-from-drive-in-colab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!