Sys.path is different than terminal and ipython and ModuleNotFoundError

Hi,

I am running jupyter notebook within a python 3.9.12 conda environment.
I have a project with the following structure

project_folder
   notebooks
       notebook_A
       ....
   module
      __init__.py
      tools
         __init__.py
         file.py

In the notebook, I try to import from module.tools.file import foo and I constantly get a ModuleNotFoundError.
But, when I try to do the import from a python terminal session or ipython everything works.

When I run import sys; print(sys.path) in the notebook I obtain

['/Users/ff/dev/project_folder/notebooks', 
'/opt/homebrew/Caskroom/miniforge/base/envs/envname/lib/python39.zip', 
'/opt/homebrew/Caskroom/miniforge/base/envs/envname/lib/python3.9', 
'/opt/homebrew/Caskroom/miniforge/base/envs/envname/lib/python3.9/lib-dynload', 
'', 
'/opt/homebrew/Caskroom/miniforge/base/envs/envname/lib/python3.9/site-packages']

and when I run the same in ipython I obtain

['', 
'/opt/homebrew/Caskroom/miniforge/base/envs/envname/lib/python39.zip', 
'/opt/homebrew/Caskroom/miniforge/base/envs/envname/lib/python3.9', 
'/opt/homebrew/Caskroom/miniforge/base/envs/envname/lib/python3.9/lib-dynload', 
'/opt/homebrew/Caskroom/miniforge/base/envs/envname/lib/python3.9/site-packages']

sys.executable points to /opt/homebrew/Caskroom/miniforge/base/envs/envname/bin/python3.9 in the notebook and to /opt/homebrew/Caskroom/miniforge/base/envs/envname/bin/python in ipython.

I am having the same issues with the imports when I run the notebook interface within VSCode (selecting the correct conda env) but not in PyCharm.

Has anybody experiencing the same issue or can anybody provide any help with this? I’ve been trying to follow most of the guides available, reinstall jupyter, etc but nothing seems to work.
Thanks!

1 Like

When you launch a notebook from JupyterLab, unless you move it after starting the kernel, the kernel’s Python module search path (sys.path) includes the notebook’s directory. If you run IPython in that directory, you’ll see that it too fails to find module.

To make the module package available, you either need to modify sys.path, or if you’re actually working on a package, you could make module a Python package and install it in editable mode. In the latter case, your choice of packaging tool will dictate the details.

1 Like