Create Conda Environment In Jupyter

1 minute read

Published:

Conda environment in Jupyter

This will show how to create customized conda environment on Mac/Linux.

Install conda

Refer to the user guide

Create new conda env

This example shows how to create new environment called my_env while specify the python version you want. The packages are optional.

$ conda create --name my_env -c conda-forge python jupyterlab ipykernel ipywidgets ipyleaflet numpy pandas scipy scikit-learn matplotlib seaborn tqdm shapely rasterio PyShp geopandas h5py xarray rioxarray plotly

Create kernel spec file (must do this inside the activated env!)

In order for Jupyter to find your kernel, run following command and optionally choose the display kernel name

(my_env) $ python -m ipykernel install --user --name my_env --display-name My-Jupyter-Env

Install packages

Before you install any package, activate the new conda env just created.

$ conda activate my_env

Then install ipykernel and other packages using conda

(my_env) $ conda install ipykernel numpy

Or using pip

(my_env) $ pip install matplotlib

launch Jupyter

Lauch Jupyter lab and you should see you new kernel my_env-jupyter from the kernel dropdown.

(my_env) $ jupyter lab
# or launch the classic notebook
(my_env) $ jupyter notebook 

remove a jupyter kernel

#check available kernels
jupyter kernelspec list
# remove selected kernel
jupyter kernelspec uninstall unwanted-kernel

Install Jupyterlab extentions

#check installed server extension
jupyter serverextension list
# install selected extention
conda install -c conda-forge jupyterlab-git

Export Conda env

# only the main packages are exported
conda env export --from-history > environment.yml 

conda env create -f environment.yml # the env name is included in the .yml file
conda activate widget
python -m ipykernel install --user --name widget --display-name widget

jupyter labextension install @jupyter-widgets/jupyterlab-manager # enable widget