Jupyter Notebook in a VirtualEnv

With more than one virtual environments, there is an additional setup if one wants to use Jupyter Notebook. In order to check whether Jupyter Notebook is using the same python executable, run the following scripts in Jupyter Notebook:

import sys


!which python3

I explicitly write python3 but if you are using python2, python may be enough. If you see different outputs for the above scripts, then there is a problem.

From now on, I followed Kernels for different environments in IPython Documentation. First, I activated a virtual environment and installed ipykernel:

source (path to a folder)/bin/activate (name)
python3 -m ipykernel install --user --name name --display-name "display name"

It seems that Jupyter uses name internally. I did not try yet without name when I activated a virtual environment. And display name is what we are seeing when we select New in Jupyter notebook, as shown below.

Screen Shot 2018-03-25 at 4.41.05 PM

I have display names “TensorFlow” and “DataScienceBowl” in addition to “Python 3”.

Once a new notebook is created, try the scripts shown at the top to be sure that the correct python executable is running. We can switch the python executable through the Kernel menu.

It seems that it may be possible to run a python executable in a different environment within a notebook. I would check this later.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s