Jack O'Neill
Jack O'Neill

Reputation: 452

Why am I unable to import multipledispatch from jupyter notebook?

I'm trying to run a jupyter notebook from within a venv environment configured using PyCharm. However, when I try to run the code from the jupyter notebook I'm getting the following error when trying to import a module I know to exist

import multipledispatch
ModuleNotFoundError: No module named 'multipledispatch'

If I open a terminal, activate the virtual environment and run the code manually in Python it works without any problems.

When I check the list of installed packages from within the notebook using

python -m pip list

the module appears as expected. If I try to install the module from within the notebook using

!python -m pip install multipledispatch

it tells me that requirements are already satisfied, however, I'm still unable to import the module.

I'm able to import other non-standard modules which I installed after creating the virtualenv, so this appears to be limited to the multipledispatch module. Does anyone have any ideas what might be causing this or how it might be fixed?

Upvotes: 0

Views: 616

Answers (1)

Jack O'Neill
Jack O'Neill

Reputation: 452

I figured out what's happening with this. Even though I'd set the python kernel to use the venv from the base project, the site-packages directory wasn't being added to the Python path.

My directory structure was as follows

  • projectDir
    • main.py
    • package1
      • package1.py
    • package2
      • package2.py
    • notebooks
      • notebook.ipynb
    • venv
      • bin
      • lib

I fixed this by manually adding the path to the project and the pip (site-packages* directory at the beginning of the notebook. I created a file called project_path.py and placed it in the same directory as the notebook.

The os.pardir reference allows me to add the projectDir to the notebook's path, this ensures that my custom packages can be called from within the notebook.

The pip_path value gives me the location of the site-packages directory for the virtual machine in which the notebook runs. By adding both of these to the python path I can import packages in my notebooks just as I can in the root dir.

import sys
import os


def fix_paths():
    module_path = os.path.abspath(os.pardir)
    pip_path = os.path.join(module_path, "venv", "lib", "python3.9", "site-packages")

    for path in [module_path, pip_path]:
        if path not in sys.path:
            sys.path.append(path)

At the beginning of each notebook I added the following to run the code and fix up the paths

from project_path import fix_paths

fix_paths()

Upvotes: 1

Related Questions