RR-
RR-

Reputation: 93

How to import custom modules in google colab? (Solution given here not working)

I'm trying to import a custom module called 'clusterer.py' into my colab notebook. But the import function is not able to find the file. Is there any other way to import custom modules?

After mounting the drive, I have already tried this approach: How to import custom modules in google colab? with the result: 'No module named 'clusterer''

!ls /content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection/*.py
!cat /content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection/clusterer.py
import sys
sys.path.append('/content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection')
import clusterer

The output is as follows: '/content/gdrive/My Drive/Colab Notebooks/Omdena_Mars_Anomaly_Detection/clusterer.py'

'/content/gdrive/My Drive/Colab Notebooks/Omdena_Mars_Anomaly_Detection/feature_extractor.py'

[Contents of the module 'clusterer.py']

ModuleNotFoundError: No module named 'clusterer'

Upvotes: 1

Views: 4229

Answers (1)

RR-
RR-

Reputation: 93

Ok, I think I figured out one solution. In my case, I had to physically go inside the directory where the files were. This is what I did

1a. Restart the kernel but 'resetting all runtimes', specially if you've just added your file.py to the directory.

1b.cd gdrive/My Drive/Colab Notebooks/Omdena_Mars_Anomaly_Detection

  1. !ls /content/gdrive/My\ Drive/Colab\Notebooks/Omdena_Mars_Anomaly_Detection/*.py

  2. !cat /content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection/mylib.py

  3. import sys sys.path.append('/content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection')

    5.import clusterer

Worked for me. Thanks

Upvotes: 1

Related Questions