100

Is there a way to use pipenv with Jupyter notebook?

Or more specifically, with an atom nteract/hydrogen python 3 kernel?

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
emehex
  • 9,874
  • 10
  • 54
  • 100

4 Answers4

192

Just tried the following with success.

In your project folder:

pipenv install ipykernel
pipenv shell

This will bring up a terminal in your virtualenv like this:

(my-virtualenv-name) bash-4.4$

In that shell do:

python -m ipykernel install --user --name=my-virtualenv-name

Launch jupyter notebook:

jupyter notebook

In your notebook, Kernel -> Change Kernel. Your kernel should now be an option.

Change Kernel Screenshot

Source: IPythonNotebookVirtualenvs

JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
Luis Meraz
  • 2,356
  • 1
  • 12
  • 8
  • 16
    Works great, thanks. To inline the virtualenv name lookup: `python -m ipykernel install --user --name=\`basename $VIRTUAL_ENV\` ` – u-phoria Jun 09 '18 at 10:46
  • This worked perfectly for me. I had to get my env name with 'which python' while in my pipenv shell – chrisfauerbach Jul 07 '18 at 00:59
  • Thanks for this solution! I wrote a little bash script to automate setting up the pipenv and adding the kernel. You can find it here: https://github.com/txoof/develtools/blob/master/pipenv_jupyter.sh – Aaron Ciuffo Dec 15 '18 at 00:44
  • 2
    is it possible to further avoid the ipykernel dependency, by using a standard kernel that somehow points to the virtualenv? I'm thinking of projects where _I_ want to use jupyter, but others may not. I'd prefer to avoid anything specific to _me_ in the pipenv deps. – ClaytonJY Apr 17 '19 at 20:27
  • 2
    Note the ipykernel can be installed with one-line commands as well: `pipenv run python -m ipykernel install --user --name=\`pipenv run basename '$VIRTUAL_ENV'\`` (standard config, venvs in ~/.venv/) `pipenv run python -m ipykernel install --user --name=\`pipenv run dirname '$VIRTUAL_ENV' | xargs basename\`` (when `PIPENV_VENV_IN_PROJECT=1`) – ClaytonJY Jun 19 '19 at 22:13
  • @Jim careful, then all your notebooks are using your global environment and all your pipenv-ing is in vain – ClaytonJY Jul 10 '19 at 19:19
  • @Jim now I'm second-guessing what I said...you should try setting up multiple virtual environments with pipenv, install different stuff in them that isn't in your global env, setup your kernels, then make notebooks with each kernel and see which packages you can/can't import. – ClaytonJY Jul 12 '19 at 21:30
  • 1
    @ClaytonJY I found out that I'm wrong, using pip install ipykernel in pipenv shell is same as pipenv install ipykernel. The only difference is pipenv install will record package to Pipfile and pip install doesn't. – Jim Jul 31 '19 at 00:33
50

Install and start jupyter inside pipenv:

pipenv install jupyter
pipenv run jupyter notebook

Any other packages that are installed via pipenv (e.g. pipenv install numpy) will also be available to your jupyter notebook session.

101
  • 8,514
  • 6
  • 43
  • 69
dahe
  • 806
  • 8
  • 13
  • This is also the most simple way for me. I just wonder, is this idiomatic? Quite contrary to how this would be done in other languages. – Kevin Wittek Mar 21 '21 at 14:20
4

Luis' answer works perfectly for jupyter notebooks.

But for hydrogen/atom specifically the recipe is:

pipenv install ipykernel
pipenv shell

launch atom from within the pipenv shell

> atom

Should be good to go!

emehex
  • 9,874
  • 10
  • 54
  • 100
4

My answer is based on previous answers, but I found one more step was necessary (pipenv install notebook). So in total:

Step 1. On terminal, first install both jupyter and (jupyter) notebook. In my experience, the latter had to be explicitly installed:

pipenv install jupyter notebook

Step 2. Install the Pipenv kernelspec for jupyter (modified from u-phoria's comment).

pipenv run python -m ipykernel install --user --name=`basename $VIRTUAL_ENV`

Now the following should work:

pipenv run jupyter notebook

Note: If in the Pipenv shell for the virtual environment, pipenv run can be removed from the above. If Pipenv is being forced to ignore virtual environments, the run commands should be run in the shell for the environment

For the original question of using Atom, it can then be run by this pipenv:

pipenv run atom
GSB
  • 51
  • 3