A virtual environment is a self-contained directory tree that contains dependencies required by different projects isolated to existing packages.
By using virtual Python environments, applications can run in their own ‘sandbox’ in isolation of other Python applications
This lets you have an isolated space on your computer for different Python projects, ensuring that each of your projects can have its own set of dependencies and modules that won’t disrupt any of your other projects. This is among the most essential tool in the bucket of any Python developer and you will use it quite often.
Why use Virtual Environments?
Let's take an example of our favorite web framework Django. Before the release of Django 2.0, all Django projects were made on the top of Python 2 which is the legacy version of Python after the Django 2.0 update Django only works with the latest Python 3 which is addressed as the future of Python.
Both the versions of the framework depends on different dependencies in such scenarios it is advisable to work with the virtual environment to set up an independent environment for projects on different versions of Django.
The virtual environment creates a directory that contains dependencies required by different projects along with some scripts. There are no limitations on the numbers of environments you can create.
Now let's take another real-world example, Imagine you have two projects A and B which depend on module C. The problem appears when we need different versions of module C, let's say module A demands v 1.0 and project B demands V 3.0 for some compatibility reason.
But python won't be able to differentiate between the different versions of a module because both the version will be stored in site package directory with the same name.
This is where you should use the virtual environment to create sperate virtual environments for both the projects, different environments can have different versions of modules.
It is a good practice to have a new virtual environment for every Python project.
This tutorial will guide you through how to create Virtual Environment for your project.
How to create Virtual Environment?
Step - 1
Open your terminal and create a directory to store all your virtual environments, using the command mkdir Environments
which is an acronym of "make directory".
Now go inside the directory using the command CD which stands for call Directory, CD Environments
Step 2
Now we will use a module named virtualenv to create isolated virtual environments.
But first, let's install this module by the following command,
pip install virtualenv
If you get an error like pip command not found then you have to install pip package manager first, you can learn this here.
To verify a successful installation run this
virtualenv --version
Now we can proceed to create virtual environments
We will create a virtual environment named myenv.
How to create Virtual Environment if you have two different versions of Python installed in your machine?
To create a Virtual Environment for Python 2.x do the following
virtualenv myenv
For a Python 3 virtual environment type -
python3 -m venv myenv
If you only have Python 3 on your machine do the following
virtualenv myenv
This will also work,
python -m venv myenv
This will create a directory myenv along with directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.
A virtual Python environment has a similar directory structure to a global Python installation. The bin directory contains executables for the virtual environment, the include directory is linked to the global Python installation header files, the lib directory is a copy of the global Python installation libraries and where packages for the virtual environment are installed, and the shared directory is used to place shared Python packages.
The interesting thing here is that these environments won't have any existing Python package or module of your computer, you can verify it by navigating into myenv>Lib>site-packages
You will only find the essential tools such as pip, easy install and setup tool, later this directory will hold all the packages and modules you will install in this particular environment and remain isolated from other environments.
To add modules and packages in our Environment, we need to activate it first.
On Windows, run:
myenv\Scripts\activate.bat
On Unix or MacOS, run:
source myenv/bin/activate
Now your command prompt will be prefixed by the Environment name which is, in this case, myenv.
This indicates that our Virtual Environment has been activated.
You can install any package here with pip or easy install these packages will be completely isolated to other environments on your device.
To deactivate the environment run the following
deactivate
Now there is no more a prefix in our terminal, which indicates that our environment has been deactivated successfully.