Skip to content

Instantly share code, notes, and snippets.

@peterhs73
Last active February 23, 2024 17:37
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save peterhs73/c9ae0d729bc4b6cebdbb8c0b87d7ab97 to your computer and use it in GitHub Desktop.
Save peterhs73/c9ae0d729bc4b6cebdbb8c0b87d7ab97 to your computer and use it in GitHub Desktop.
Python Virtual Environments and Jupyter Notebook Kernel Guide

Create Python Virtual Environment and Install iPython Kernel for researchers working with Jupyter notebooks.

TL;DR

  • Virtual Environment
    • Virtualenv
      virtualenv env_to_create  
      \folder_to_create\Scripts\activate
      
    • Miniconda
      conda create -n env_to_create python=x.x  
      conda activate env_to_create
      
  • iPython Kernel (while virutal environment activated)
    (env_to_create) pip install ipykernel
    (env_to_create) python -m ipykernel install --user --name=new_kernel_name
    

The long version

The idea:
You would want a contained environment for python code development or jupyter notebook development.

A simple way to do it is to create a virutal environment - basically an isolated (standalone) folder with python intepreter installed in the folder. All the python code and jupyter notebook will run within the folder/environment.

Virtual environment

Virtual environment with virtualenv package

In the parent folder, create the desired virtual enviroment folder.

virtualenv env_to_create

Activate the virtual environment from the parent folder

\folder_to_create\Scripts\activate (on Windows)

source /folder_to_create/bin/activate (on Mac and Linux)

To choose a python version is very tedious, which involves manually install other python versions on the system first. More details on this can be found here, basically need to indicate the path to installed python when create the virutal environment.

Other env related packages and modules

Virtual environment with miniconda (works for anaconda)

With miniconda (or anaconda installed):

conda create -n env_to_create python=x.x

[note] add anaconda at the end the command would install some of the basic packages from anaconda (actually a lot of them), sometimes you don't want to include those packages but to install packages manually.

To Activate your virutal environment:

conda activate env_to_create

[note] create virtual environment with conda will create a environment folder under your system miniconda (or anaconda) folder: normally miniconda/env/env_to_create. You can change the default virtual env folder to somewhere else, but not necessary in most of the cases.

The advantage of conda and current state of pip summarized in myth #6.

Deactivate

To exist out the virutal environment:

deactivate

Install kernel for Jupyter Notebook

In short, jupyter (ipython) notebook doesn't care about where the directory or the environment is. Instead, it tracks the environment.

Install the ipython kernel while virtual environment activated, name will be displayed in jupyter notebook under kernel.

(env_to_create) $ pip install ipykernel
(env_to_create) $ python -m ipykernel install --user --name=new_kernel_name

To see all the kernels avalible and their environment:

jupyter kernelspec list

Additional Source:

virtual environment with conda
python environments
Install Jupyter Notebook Folder
ipython kernels

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment