简体   繁体   中英

ImportError: No module named ... in Colab google

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

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/'])

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 waring error: 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.

%cd /content/models/research/slim
!python setup.py build
!python setup.py install
%cd /content/models/research/deeplab

doing this got me solved...

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