Importing a python module from the python script f

2019-08-04 04:48发布

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条回答
做自己的国王
2楼-- · 2019-08-04 05:33

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
查看更多
登录 后发表回答