104

Is there a way to use pipenv with Jupyter notebook?

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

5 Answers 5

207

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

Sign up to request clarification or add additional context in comments.

8 Comments

Works great, thanks. To inline the virtualenv name lookup: python -m ipykernel install --user --name=`basename $VIRTUAL_ENV`
This worked perfectly for me. I had to get my env name with 'which python' while in my pipenv shell
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.
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 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.
|
53

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.

1 Comment

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

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!

1 Comment

For use here...I still had to do the python -m ipykernel install --user --name=my-virtualenv-name command.
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

Comments

-1

In previous answers is assumed that IPython is part of Pipenv. If it is not and you want
to work in Pipenv from Jupyter Notebook do:

pipenv install ipykernel
pipenv shell

ipython kernel install --name=`basename $VIRTUAL_ENV` --user

jupyther notebook
# select .venv kernel

Note that ipython installation is made.

5 Comments

This just repeats the highly-voted, seven and a half year old, accepted answer with less detail. Please don't repeat answers.
Sorry. Where is pointed out that ipython could be used?
That answer uses slightly different syntax to invoke the same command: python -m ipykernel install....
It doesn't work for me. I've tried. May be because ipython is outside the pipenv. By the way in the given URL (help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs) ipython is used.
ipython probably shouldn't be outside the virtual environment. In any case, that's a detail of how you've set up your system, not something inherent to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.