What is a Virtual Environment ?
A virtual environment is a tool that helps developers to segregate and maintain the dependencies required by different projects by creating isolated python virtual environments.
Need for Virtual Environment ?
Suppose, User A and User B are working on 2 projects.
The package requirements for user A is given below
Click==7.0 Flask==1.0.2 Flask-Cors==3.0.7 itsdangerous==1.1.0 Jinja2==2.10 MarkupSafe==1.1.0 six==1.12.0 Werkzeug==0.14.1
Also the package requirements for user B is given below.
Click==6.0 Flask==1.0.1 Flask-Cors==3.0.2 itsdangerous==1.1.0 Jinja2==2.10 MarkupSafe==1.1.0 six==1.11.0 Werkzeug==0.11.2
As you can see, the two developers use different versions of similar packages. Python does not have the ability to differentiate between multiple versions of the same package in the site-packages directory. By default the package installation happens in the default site-packages directory of the python installation. In Unix like operating system, by default the location will be owned by the root user and a normal user will not be able to perform a package installation without elevated privileges.
Virtual environment plays its role in the following scenarios
- To isolate the dependencies required for different projects
- To maintain the base python packages untouched. In case of multi-user environment, upgrading or modifying a package might disrupt the operation of one or more projects.
- To enable easy access for the installation and management of python packages to the end users without enabling system level elevated privileges.
- To easily manage the dependencies used in a specific project. We can copy the virtual environment to another system of the same version. Also it is easy to replicate the environment by dumping the package list (pip freeze).
How to create a virtual environment ?
For creating a virtual environment, we need the virtual environment package installed in the base python environment.
As root user or with elevated privilege, execute the following command
pip install virtualenv
Then create the virtual environment with the following command. The virtualenv command will be available only if the package was installed in the base python.
virtualenv <path for virtual environment>
You can specify any writable location as the path for virtual environment. All the virtual environment related files and packages will be installed in this directory. It may take few seconds complete the virtual environment setup.
Now type
which python
this will be still pointing to the base python. For using the virtual environment, we need to activate the environment.
source <path of virtual environment>/bin/activate
The above command will activate the virtual environment in the current session. For making it enabled in all sessions by default, add these lines in the .bashrc file.
Now again type which python check the result. It will be pointing to the newly created virtual environment.
For deactivating the environment, simply type deactivate in the command line.