Python Visual Studio Code Jupyper ModuleNotFound

Assume, there is a following code in Python that needs to be executed in Jupyter notebook in Visual Studio Code.

import somemodule
import matplotlib.pyplot as plt
msg = "hello world"
print(msg)

When executing this code you might get following error:

ModuleNotFoundError Traceback (most recent call last)
in

----> 1 import somemodule

ModuleNotFoundError: No module named 'somemodule'

The reason for this is that Jupyter Server does executes in an environment, which does not contain the module. So, you will have to install the module in the correct environment.

To find out which python version is used by Jupyter Server I execute following code in the Jypyter cell:

print(sys.executable)

This code outputed following:

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python.exe

As next, I navigated to the folder with the Python binary:

cd C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64

And executed following:

python -m pip install matplotlib --user

Note the --user. If you don't specify it, you will get Access Denied, because Windows does not allows you to write to C:\Program Files (x86).

480_brain_icon_gc


comments powered by Disqus