How to create a virtual environment in Python?

Python virtual environment

A Python virtual environment is a virtual environment such that the libraries, packages and scripts installed into it are isolated from other virtual environments or the default Python environment i.e. the ones which are installed on your operating system. Virtual environments are really useful and needed by the developers. The different projects require different libraries or dependencies. Thus, it is beneficial to isolate the dependencies of one project from the other .

Suppose we have two different projects which require different versions of the same library. Now, by default, both the versions of the library will be residing in the same directory. Python won’t be able to decide which version of library to use for which project. Thus, Virtual environments play very important role in such scenarios. The problem can be solved by having separate virtual environments for both the projects and hence keeping the library requirement of one project isolated from the other. We can create any number of virtual environments. It is generally good to create a new virtual environment for each project.

Creating a virtual environment

The virtual environment can be created using a tool named virtualenv. This tool is used to create different virtual environments with required Python packages for each project.

You need to install virtualenv at first. The following command is used to install virtualenv. You need to have pip preinstalled .

pip install virtualenv

After running the command, check if virtualenv is installed on your system. The following command displays the version of virtualenv if it is successfully installed on the system.

virtualenv –version

Once virtualenv is installed, we can use it to create virtual environments for our projects. Creating a virtual environment through virtualenv is quite easy. The following command serves the purpose.

virtualenv environment_name

The environment_name here specifies the name of the virtual environment created. You may give name of your choice. This command will create a directory named “environment_name” in this case.

After creating the virtual environment, we need to activate it to start using this particular environment for our project.

To activate a virtual environment, run the following command

$ source virtualenv_name/bin/activate

Here virtualenv_name is the name of the environment you want to activate. Once activated, the name of the active environment will be displayed on the left side of the terminal. You can download required libraries or dependencies in this active environment and hence these libraries will be isolated from other environments. You can download and perform specific tasks in this virtual environment. Once you are done with all the work of this particular environment, you can deactivate this virtual environment with the following command.

(virtualenv_name)$ deactivate

After running this command, you will enter the default system environment of Python.