1

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
neves
  • 33,186
  • 27
  • 159
  • 192
  • 1
    I'd suggest trying the current best practice installation via the `%pip install pandas`. It may not work if you've complicated your system, but in general it is the version you should be using going forward. See more about the modern `%pip install` command [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez). That version should 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 & that's why the magic version was added. – Wayne Mar 17 '23 at 03:59
  • [This comment by waterproof](https://stackoverflow.com/questions/38368318/installing-a-pip-package-from-within-a-jupyter-notebook-not-working#comment101990468_45243894) makes me hopeful that suggestion will work. And since I started another comment, I'll add a reference for why the exclamation point is now disfavored for use with `pip` (and also `conda install`): see [the second paragraph](https://discourse.jupyter.org/t/location-of-libraries-or-extensions-installed-in-jupyterlab/16303/2?u=fomightez) about why the exclamation point use with `pip` can be an issue. – Wayne Mar 17 '23 at 04:07
  • 1
    @Wayne you'd you please add your comment as an answer? I didn't know about the `%pip` magic – neves Mar 17 '23 at 15:46

1 Answers1

2

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.


Additional information on why these days with pip and conda you are better off with no symbol in front than using an exclamation point

In 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.

Wayne
  • 6,607
  • 8
  • 36
  • 93
  • It works. Running just `pip` will use the pip from my virtual environment. Thanks. – neves Mar 17 '23 at 19:00
  • Inside the notebook? It should be adding the `%` behind-the-scenes. Hmmm...maybe that's not 100% foolproof? You found with the magic symbol it was correct though? Maybe I should remove that part of the discussion. – Wayne Mar 17 '23 at 19:57
  • I think I misunderstood maybe? You seem to say automagics does work trustworthy [here](https://stackoverflow.com/a/75771341/8508004). – Wayne Mar 19 '23 at 18:35