ModuleNotFoundError confusion when running from different locations

  jupyter-notebook, python

I know this is going to be a trivial issue but for the life of me I cannot figure it out.

I have the following code directory structure for a Python project called CA (simplified for this question):

CA/
  Python/
    __init__.py
    src/
      interval.py
      es.py
    notebooks/
      analysis.ipynb

es.py is a class that inherits from interval.py (interval class), so within es.py, I import the interval class with from Python.src.interval import *, which works just fine when running a script.

However, when I try and run the analysis notebook (analysis.ipynb), I add src directory to the python path with sys.path.append("../src/"), but it fails when the notebook loads es.py, which subsequently tries to run the import statement from Python.src.interval import * and says ModuleNotFoundError: No module named ‘Python’.

I have confirmed that the CA root directory is already in the PYTHONPATH.

If I remove the Python.src prefix, the ipynb works, but then a normal script does not, so I’ve had to resort to adding and removing it constantly based on the current task.

Seriously doing my head in trying to figure this directory structure out, so any insight would be appreciated.

Source: Python Questions

LEAVE A COMMENT