Body
Summary
This article details the steps for adding your Anaconda Python virtual environments to Jupyter Notebook, so that they show up as alternative notebook choices within Jupyter's dropdown menu.
Environment
- macOS
- Linux
- Anaconda Python
- conda 4.7.12 and above
- Jupyter Notebook
Directions
You can add the environments to Jupyter Notebook for your individual user account, or instead for the whole system. Adding for the whole system would be more fitting on a system that has multiple users accessing the computer. Assuming your default environment is called (base)
, this will detail how to add one of the virtual environments created under Anaconda Python to the dropdown list in Jupyter Notebook. The python package ipykernel is required to make these changes. It should be installed by default with anaconda, and it will need to be installed in each of the environments you would like to add. To see if it is available for the environment you are in, type:
(base)$ conda list ipykernel
If for some reason it is not installed in any of the environments, if installing as a single user you can easily add it with:
(base)$ pip install --user ipykernel
Or you can install it for the system with:
(base)$ conda install ipykernel
Repeat this process for each of the environments you would like to add to your (base)
Jupyter notebook dropdown, by first activating the environment, and then installing with the appropriate commands above.
(base)$ conda activate python2
Single User Instructions
- First you should get a list of the environments available on your system. In the Terminal:
(base)$ conda env list
The output may resemble something like this
# conda environments:
#
base * /usr/local/anaconda
python2 /usr/local/anaconda/envs/python2
ciao /usr/local/anaconda/envs/ciao
- To add the python2 virtual environment, using terminal, you'll first need to switch into the environment you would like to add to Jupyter Notebook:
(base)$ conda activate python2
- Then to add the environment, type:
(python2)$ python -m ipykernel install --user --name=python2
Alternately, the following command also works to add your environment to jupyter notebook:
(python2)$ ipython kernel install --user --name=python2
- Now go back into the
(base)
environment:
(python2)$ conda deactivate
- Test if both the base and python2 environment show up correctly:
(base)$ jupyter kernelspec list
On a Mac it should return something like this:
Available kernels:
python2 /Users/johnsmith/Library/Jupyter/kernels/python2
python3 /usr/local/anaconda/share/jupyter/kernels/python3
- Now run jupyter notebook from the
(base)
environment:
(base)$ jupyter notebook
- Now when you click the New dropdown in the upper right of the Jupyter notebook browser window, it will show you the available environments.
- Repeat the process for any of the other environments you would like to add, substituting that name in for
python2
in the above steps.
Whole System Addition
If you want to install it for the whole system, you would need to execute these commands as the root user. Modify the command where you drop the --user
, so the command would be python -m ipykernel install --name=python2
or ipython kernel install --name=python2
. Please note that, if the root user's prompt does not display the (base)
in the front, you will need to run /usr/local/anaconda/bin/conda init
as root, as well as then running source .bash_profile
within root's home directory.
Remove Environment
You can also remove an environment as well, especially if for some reason you've removed the environment from your system. You can run the command from the (base)
environment. To remove it from your Jupyter notebook dropdown selection:
(base)$ jupyter kernelspec uninstall python2
External resources
https://janakiev.com/blog/jupyter-virtual-envs/
https://www.geeksforgeeks.org/using-jupyter-notebook-in-virtual-environment/