HOWTO: Use a Conda/Virtual Environment With Jupyter

The IPython kernel for a Conda/virtual environment* must be installed on Jupyter prior to use.

*See create conda/virtual environment if there is not already an environment that has been created.

Install kernel

To perform the kernel installation, users should load the preferred version of Python*

module load python
* Change to the proper version of python (or the version that you created your conda/venv with)
* Use module spider python to view available python versions

and run one of the following commands, depending on how your Conda/virtual environment being created. Make sure to replace MYENV with the name of your conda environment or path to the environment.

if the Conda environment is created via conda create -n MYENV

~support/classroom/tools/create_jupyter_kernel conda MYENV

if the Conda environment is created via conda create -p /path/to/MYENV

~support/classroom/tools/create_jupyter_kernel conda /path/to/MYENV

if the Python virtual environment is created via python3 -m venv /path/to/MYENV

~support/classroom/tools/create_jupyter_kernel venv /path/to/MYENV

Install Jupyterlab Debugger kernel

According to Jupyterlab page, debugger requires ipykernel >= 6. Please create your own kernel with conda using the following commands:

$ module load miniconda
$ conda create -n jupyterlab-debugger -c conda-forge "ipykernel>=6" xeus-python
$ ~support/classroom/tools/create_jupyter_kernel conda jupyterlab-debugger

You should see a kernelspec 'conda_jupyterlab-debugger' created in home directory. Once the debugger kernel is done, you can use it:
1. go to OnDemand
2. request a JupyterLab app with kernel 3
3. open a notebook with the debugger kernel.
4. you can enable debug mode at upper-right kernel of the notebook

Remove kernel

If the envirnoment is rebuilt or renamed, users may want to erase any custom jupyter kernel installations.

Be careful! This command will erase entire directories and all files within them.
rm -rf ~/.local/share/jupyter/kernels/${MYENV}

Manually install kernel

If the create_jupyter_kernel script does not work for you, try the following steps to manually install kernel:

# change to the proper version of python
module load python  

# replace with the name of conda env           
MYENV=useful-project-name

# Activate your conda/virtual environment
## For Conda environment
source activate $MYENV

# ONLY if you created venv instead of conda env
## For Python Virtual environment
source /path/to/$MYENV/bin/activate

# Install Jupyter kernel 
python -m ipykernel install --user --name $MYENV --display-name "Python ($MYENV)"

 

Supercomputer: 
Service: 
Fields of Science: