Search code examples
pythonpackagegoogle-colaboratoryimporterror

Colab ImportError for self-developed package


I tried to import DataPreprocessor class in dgnfiai/gdnfiai/preprocessing/preprocessing.py my self-developed package gdnfiai into colab notebook but got ImportError: cannot import name 'preprocessing' from 'gdnfiai' (unknown location). The package can be successfully imported on my laptop. I think maybe the problem is colab's default directory so I changed the directory on colab using

import sys
sys.path.append('/content/gdnfiai/')

but still got the same error.

The structure of the package and my code for import is shown in the screenshot here:enter image description here

The exact steps I did are:

  1. Open pipeline.ipynb notebook in colab. pipeline.ipynb is in gdnfiai/scripts folder in my package. But I uploaded it separately because I cannot run the notebook from cloned git repository.
  2. Clone git repository gdnfiai.
  3. sys.path.append('/content/gdnfiai/')
  4. pip install -e .
  5. Run from gdnfiai.preprocessing.preprocessing import DataPreprocessor and got ModuleNotFoundError.

Solution

  • It might be due to your typo in the preprocessing directory, with the filename __inti__.py. Rename it to __init__.py, as in your other directories.