Search code examples
pythontensorflowimporterrorgoogle-colaboratory

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


Solution

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