Create Python Virtual Environment and Install iPython Kernel for researchers working with Jupyter notebooks.
- 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
- Virtualenv
- 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 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.
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
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.
To exist out the virutal environment:
deactivate
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
virtual environment with conda
python environments
Install Jupyter Notebook Folder
ipython kernels