I've created a Python venv and created a JupyterHub kernel based in it.
Everything works fine, I can start the kernel from JupyterHub interface and it is using my desired Python interpreter and venv.
The problem is that the directory with the global python
and pip
are first in the PATH variable.
When I try to run !pip install pandas
it finds the global pip
instead of my venv pip. If I run !which python
it finds my system python, instead of my venv python, so !python -m pip install pandas
also does not work.
I know that I can open a terminal, activate the venv and install my packages, but my users are confused. They are used to just run pip install in a notebook cell.
How to I make the command below work in my venv based kernel notebook?
!pip install some-package
I'd suggest trying the current best practice installation via the %pip install
magic command. Specifically for your case:
%pip install pandas
See more about the modern %pip install
command here. The magic version of the command insure the installation occurs in the environment that is backing the kernel that is active in the notebook.
The exclamation point doesn't necessarily do that and that's why the magic version was added. The second paragraph here goes into more details about why the exclamation point may lead to issues.
Indeed, this comment by waterproof here makes me think the %pip install
magic command helps when using things like venv, too.
pip
and conda
you are better off with no symbol in front than using an exclamation pointIn fact, because in most modern Jupyter installations automagics is on be default and that is why these days pip
and conda
from inside the notebook will work better if you leave if any symbol, compared to including the exclamation point. Without any symbol automagics will insure the magic versions of the commands are used. Always best to be explicit though and include the magic command so that you and others know what was happening clearly.
But in contrast to all the outdated answers out there, without any symbol in general is now going to yeild a better experience than using the exclamation point for pip
or conda
install steps in Jupyter.