问题
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