0

In a project where I have to run some Jupyter notebooks, I created a virtual environment using pipenv and installed some packages (note that I used the --site-packages flag).

Although now I am now able to run the notebooks with pipenv run papermill ..., I cannot run them from Jupyter using pipenv run or pipenv shell because of some ModuleNotFoundError exceptions.

In particular, the modules that are note found in the second case are the ones installed in the virtual environment only and not inherited from global-sites.

Indeed, if I check the sys.path I can see the difference in the two cases: in the second there is no ~/.local/share/virtualenvs/... entry.

Why am I having this issue and how can it be solved? (If possible, I would prefer not to pollute my ~/.local/share/jupyter/kernels with other kernels from virtualenvs).

  • Probably answered [at this discussion](https://stackoverflow.com/questions/42449814/running-jupyter-notebook-in-a-virtualenv-installed-sklearn-module-not-available) – Andrei R. Aug 10 '20 at 18:33
  • @AndreiR. I can confirm that jupyter is installed in the virtualenv and the output of `which jupyter` is the correct path indeed. So unfortunately the answers in the discussion don't solve the issue. – Stefano Campanella Aug 11 '20 at 10:31

1 Answers1

0

As was suggested here, you also need to make sure that the kernel is also under the venv:

python -c "import IPython"
python -m ipykernel install --user --name=my-virtualenv-name

and then switch the kernel named "my-virtualenv-name" in the jupyter user interface

Andrei R.
  • 166
  • 1
  • 2
  • Thanks for the answer. However, as I wrote in my question, I would prefer not to populate `~/.local/share/jupyter/kernels` with kernels from virtual environments. Are you suggesting that this is the only way of solving the issue? – Stefano Campanella Aug 12 '20 at 12:31