Python Virtual Environment | Introduction

Python Methods and Functions

Virtual Environment — it is a tool that helps you separate the dependencies needed for different projects by creating isolated Python virtual environments for them. It is one of the most important tools that most Python developers use.

Why do we need a virtual environment?

Imagine a scenario where you working on two Python web projects, one using Django 1.9 and the other — Django 1.10 and so on. In such situations, the virtual environment can be really useful to maintain the dependencies of both projects.

When and where to use the virtual environment?

By default, each project your system will use these same directories to store and retrieve site packages (third party libraries). What does it matter? Now, in the above example of two projects, you have two versions of Django. This is a real problem for Python as it cannot differentiate between versions in the "site-packages" directory. So v1.9 and v1.10 will be in the same directory with the same name. This is where virtual environments come into play. To solve this problem, we just need to create two separate virtual environments for both projects. The great thing about this is that there is no limit to the number of environments you can have, as these are just directories containing multiple scripts.

The virtual environment should be used whenever you work on any project based on Python. It is generally good to have one new virtual environment for every Python-based project you work on. Thus, the dependencies of each project are isolated from the system and from each other.

How does the virtual environment work?

We use a module named virtualenv, which is a tool for creating isolated Python environments. virtualenv creates a folder that contains all the required executables to use the packages required for the Python project.

Installing virtualenv

 $ pip install virtualenv 

Check your installation:

 $ virtualenv --version 

Using virtualenv

You can create a virtualenv with the following command:

 $ virtualenv my_name 

After executing this command, a directory named my_name will be created. This is the directory that contains all the required executables to use the packages required for the Python project. Python packages will be installed here. 
If you want to specify a Python interpreter of your choice, such as Python 3, you can do so with the following command:

 $ virtualenv -p / usr / bin / python3 virtualenv_name 

To create a Python 2.7 virtual environment, use the following command:

 $ virtualenv -p /usr/bin/python2.7 virtualenv_name 

Now after creating virtual environment, you need to activate it. Remember to activate the appropriate virtual environment every time you work on a project. This can be done with the following command:

 $ source virtualenv_name / bin / activate 

After activating the virtual environment, the name of your virtual environment will appear on the left side of the terminal. This will let you know that the virtual environment is currently active. In the image below, a virtual environment named venv is active. 
Now you can install the project related dependencies in this virtual environment. For example, if you are using Django 1.9 for a project, you can install it in the same way as other packages.

 (virtualenv_name) $ pip install Django == 1.9 

The Django 1.9 package will be placed in the virtualenv_name folder and isolated from the entire system. 
Once you're done, you can deactivate the virtual environment with the following command:

 (virtualenv_name) $ deactivate 


You are now back to your default Python installation.

This article is courtesy of Mayank Agrawal . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting the article [email protected] ... See my article appearing on the Python.Engineering homepage and help other geeks.

Please write in comments if you find anything wrong or if you'd like to share more information on the topic discussed above.





Tutorials