Tutorial — Python Virtual Environment

With Python Virtual Environment — virtualenv, projects can be developed independently without interference from various versions of packages.

I-No Liao
I-No Liao
Sep 2, 2018 · 3 min read

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 virtualenv

2. 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 --version

and

$ which python3

The output should look like

/usr/local/bin/python3

Navigate to the directory of our project, enter

$ virtualenv -p /usr/local/bin/python3 env

where “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/activate

Now 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

$ deactivate

3. 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.txt

Deactivate 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.txt

4. 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 ipykernel

Install 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.

Example of how kernel.json access to the corresponding virtual environment

Launch Jupyter Notebook by

$ jupyter notebook

We can now either create a notebook powered by preferred kernel or switch to preferred kernel on existing notebooks.

Example: New a notebook powered by “yourEvnName” virtual environment.
Example: Switch to preferred kernel on a existing notebook.

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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade