virtualenv
serves for creating isolated Python environments.
It creates a folder with a copy of Python interpreter and a copy of the pip
library which will be used to install other packages in this virtual environment without affecting other projects or system-wide libraries.
virtualenv
makes it easier to work on more than one project at a time without introducing conflicts in their dependencies.
From this article you will find out how to install, create, activate and deactivate virtualenv
.
Cool Tip: How to install pip
on MacOS, Ubuntu, CentOS! Read more →
Install Virtualenv
Install pip
:
$ sudo apt install python3-pip
Install the latest versions of virtualenv
using pip
:
$ sudo pip install --upgrade virtualenv
Create Python Virtualenv
Create a venvs
directory to store all virtual environments:
$ mkdir venvs
Create a new virtual environment for your project:
$ virtualenv venvs/<project-name>
Set Python version in virtualenv
, if you want to use different Python version:
$ virtualenv -p /usr/bin/python2.7 venvs/<project-name>
(De)activate Python Virtualenv
Activate virtualenv
:
$ source venvs/<project-name>/bin/activate
Deactivate virtual Python environment:
$ deactivate