P i
P i

Reputation: 30674

How to create IPython/Jupyter kernel with custom python environment

I have a virtual environment containing a package I need to test in a notebook.

I've got it working in the following manner:

> pip install ipykernel

> ipython kernel install --user --name=foo
Installed kernelspec foo in /Users/pi/Library/Jupyter/kernels/foo

> cat /Users/pi/Library/Jupyter/kernels/foo/kernel.json
{
 "argv": [
  "/usr/local/anaconda3/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "foo",
 "language": "python"
}

Now I edit the python path in that file to /path/to/my/virtualenv/bin/python.

Now I do jupyter notebook -> new -> foo, and create a test-cell with import MyPackage, and it works!

My question is: is there a proper way to do this?

(Useful link: https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/)

Upvotes: 0

Views: 973

Answers (1)

Nick Martin
Nick Martin

Reputation: 729

Personally, I would use pipenv.

First create a new folder and cd to it:

mkdir [name-of-my-python-env]
cd [name-of-my-python-env]

Now install the environment along with any additional packages you need:

pipenv install jupyter foo bar

Start the notebook server:

pipenv run jupyter notebook

If you need to add any new dependencies just pipenv install them and restart your server.

Upvotes: 1

Related Questions