Search code examples
pythonmodulegoogle-colaboratorymodulenotfounderror

ModuleNotFoundError in Colaboratory


I am trying to run my project using the gpus but I can't get it to work. I have ran the following commands:

from google.colab import drive
drive.mount('/content/gdrive')

%cd gdrive/MyDrive/project_folder

import sys
sys.path.append('/content/gdrive/MyDrive/project_folder')

I then try to run my main script from project_folder by using

! python property_prediction/predict.py

In the first line of predict.py I import a module from the folder 'project_folder' but that gives this error in colab:

File "property_prediction/predict.py", line 17, in <module>
from GP.kernels import Shortest_Path
ModuleNotFoundError: No module named 'GP

Why is it not finding the folder GP which contains my kernels script?


Solution

  • Try to replace your running command with:

    !python -m property_prediction.predict
    

    Or better:

    from property_prediction.predict import predict # or whatever your main function is called
    
    predict()
    

    NB: This is of course assuming that you have a module named GP in the folder project_folder

    If none of this work, you might be interested in reading this or other articles about imports with python (this is most likely not a problem of google collab)