问题
I'm following the tutorial here (Object Detection in Google Colab with Custom Dataset). The first line of the notebook is a git clone of the tensorflow models:
!git clone --quiet https://github.com/tensorflow/models.git
After, they set the PYTHONPATH variable to be sure we can import models.
os.environ['PYTHONPATH'] += ':/content/models/research/:/content/models/research/slim/'
If I try at this stage to import a model
from nets import inception_resnet_v2
I get the error:
ImportError: No module named nets
I checked and nets folder and nets/inception_resnet_v2.py file are there (in models/research/slim folder). I suspect that it's related to the colab naming convention because the pwd command gives:
/root/models/research
I substituted content for root in the above command but it does not work either. Someone here posted a similar question but the only answer refers to tensorflow issue 1832 which is not the problem here. Can someone help?
EDIT: operating system is Linux-4.14.79+-x86_64-with-Ubuntu-18.04-bionic
回答1:
The Python process reads the value of PYTHONPATH
at startup, so modifying that environment variable while the process is already running will not change where that process looks for packages. You should instead adjust the value of sys.path
:
import sys
sys.path.extend(['/content/models/research/', '/content/models/research/slim/'])
回答2:
The solution depends on your operation system, Linux or Windows. Someone has already asked the same question: tutorialTensorflow object detection: ImportError: No module named nets. Tensorflow object detection: ImportError: No module named nets
If you use Windows, changing the PYTHONPATH may not work. Here is a try.
First, run the file setup.py
.
python setup.py build
python setup.py install
and it may give you a waringerror: could not create 'build'
(because the file has already existed). Because there is a file named "build" in what you git clone. However, the command "build" and "install" need to make a new folder named "build" . I do not know what the file "build" is used for, so I choose to move the file to another directory and use the command above, and it will work.
来源:https://stackoverflow.com/questions/57257530/importerror-no-module-named-in-colab-google