Tutorial — Python Virtual Environment
With Python Virtual Environment — virtualenv, projects can be developed independently without interference from various versions of packages.
Published at 2018/09/02
During project development, one annoying issue is that once packages are updated to new versions, those packages are no longer compatible with previous projects, causing troubles when running the program.
Python virtual environment, virtualenv, can solve the problem by constructing independent environment for each project to avoid conflicts.
In addition, using virtual environment allows projects to be easily transferred or shared to other developers. We only have to copy the entire project and activate its built-in virtual environment without being troubled by installing all required packages for the project.
1. Install Python Virtual Environment
$ pip3 install virtualenv2. Create Virtual Environment
Specific version of Python should be specified when creating a new virtual environment. To check the default Python version and path, enter
$ python3 --versionand
$ which python3The output should look like
/usr/local/bin/python3Navigate to the directory of our project, enter
$ virtualenv -p /usr/local/bin/python3 envwhere “env” is the self-defined virtual environment name.
At this point, we have initiated a virtual environment. Let’s activate it by
$ source env/bin/activateNow the terminal is prefixed by the name of the activated virtual environment, and it should look like
(env) I-Nos-MacBook-Pro:myProject ino$With “(env)” being shown, it means we have entered virtual environment. Now we can install any packages we want without contaminating the system or other projects.
To leave the current virtual environment, enter
$ deactivate3. Copy Virtual Environment from One to Another
Within the existing virtual environment, we can generate a list enumerating all installed packages. Based on the list, pip can help us install all listed packages on another virtual environment.
To generate the list, activate the existing virtual environment and enter
$ pip3 freeze > packageList.txtDeactivate the current virtual environment. Copy “packageList.txt” to the directory of a new project where we would like to install the packages. Activate the new virtual environment and enter
$ pip3 install -r packageList.txt4. Launch Jupyter Notebook on Virtual Environment by Selecting Kernel
Launch Jupyter Notebook while the virtual environment being activated does not mean we enter the corresponding virtual environment. Instead, we should create a new kernel that contains all packages of a preferred virtual environment and switch to that kernel on Jupyter Notebook.
In order to do so, activate virtual environment first and install Jupyter and ipykernel by
$ pip3 install jupyter
$ pip3 install ipykernelInstall current virtual environment to a new kernel by
$ python3 -m ipykernel install --user --name yourEnvName --display-name "yourEnvName"The terminal will prompt the kernel location for reference. For example, my kernels are stored at : “/Users/ino/Library/Jupyter/kernels/”. Enter kernel folder and open “kernel.json”. We can check how the kernel access our environment.

Launch Jupyter Notebook by
$ jupyter notebookWe can now either create a notebook powered by preferred kernel or switch to preferred kernel on existing notebooks.


Summary
Python virtual environment not only prevents contamination of numerous package versions, but allows us to rapidly transfer or share our projects to other developers.
