Jupyter Notebook in a virtual environment (virtualenv)

I love Jupyter Notebook. I use it all the time.

I’ve been looking to use it with a Django App to profile timing and memory usage of specific function but first I had to get it running in a virtual environment.

Make sure you have virtualenv installed

pip install virtualenv

Create a virtual environment

Note: I like to use a . in front of my virtual environments so the folder is hidden.

virtualenv .venv

Get inside the environment

source .venv/bin/activate

or in shorthand

. .venv/bin/activate

You can verify quickly you are in the environment by running which python which will return the path of the python executable in the environment if all went well

which python
> xxxxx/.venv/bin/python

Install a Jupyter kernel

This will install a kernel inside the environment, to use to run in the Jupyter notebook there:

ipython kernel install --user --name=.venv

Run Jupyter, and select the .venv kernel to run the notebook

jupyter notebook
Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store