Creating Your Python Virtual Environment

Working on a virtual environment can save your system and your works on it. Creating your Python virtual environment would ensure that the dependencies required for different projects are in separate environments.

It is always recommended that you create a separate virtual environment for separate projects so that there are no clashes in dependencies requirements.

For example, a project may require PyQt5 while the other would require earlier versions of it.

Here I am going to share how I created it on my Raspberry Pi.

Install Virtualenv and Virtualenvwrapper

$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip

Update ~/.profile file

To make the system understand that the above packages, copy-paste the following lines and press Return or Enter.

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

Run the ~/.profile file

Before using or creating the virtualenv, run the following command:

source ~/.profile

Create the Virtual Environment

Say you want to want to create a virtual environment for a computer vision using OpenCV with the name cv2. To perform this:

mkvirtualenv cv2 -p python2

If your Python version is 3.x:

mkvirtualenv cv2 -p python3

Done!

How to Use the Virtual Environment

To use the environment you just created:

$ source ~/.profile
$ workon cv2

1 thought on “Creating Your Python Virtual Environment”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top