Virtual Enviroment in Python




A virtual environment is a tool for isolating Python environments for individual projects, which helps to maintain separation between the various dependencies that may be needed by various tasks. Most Python programmers consider this a crucial piece of equipment.

Each and every one of your projects will automatically use different folders as their primary source for installing and updating their respective site packages (third-party libraries). Since Python cannot tell the difference between versions in the “site-packages” directory, this is a major issue. This means that for example both versions 1.10 and 1.9 would share the same directory. For this reason, we can make use of virtual environments. A simple solution to this issue is to set up two independent virtual environments, one for each project. The beautiful thing about having environments that are simply directories with some scripts is that you can have as many as you like. When working on a Python-based project, it’s best to do it in a virtual environment. Each every Python-based project benefits from its own isolated virtual environment. Therefore, each project’s dependencies can be kept completely separate from the rest of the system and from one another.

Advantages of a virtual environment

  • Virtual environments allow us to create a separate setting for each project’s development, so we can use them to set up only the software we need.
  • Since each project’s virtual environment is separate and secure, it’s possible for them to use multiple versions of the same library, as needed. You can set up a development environment and install libraries without worrying about your system’s rights when working in a virtual environment.
  • Project dependencies can be recorded in a “requirements.txt” file after packages have been installed in a virtual environment. This makes it easy for other developers to pick up where you left off on the project and install all the necessary packages with a single command.


To set up multiple Python environments, we utilize a module called virtualenv. Virtualenv generates a directory with all the executables needed to run the Python packages required for a given project.

Installing virtualenv
$ pip install virtualenv

Test your installation:

$ virtualenv –version
Using virtualenv

Use the following command to set up a virtual environment:

$ virtualenv my_name

If you run this command, a folder with the name “my_name” will be created. Here you will find the Python project’s executables for using the required packages and Python add-ons will be set up. The following command allows you to choose the Python interpreter of your choice, such as Python 3:

$ 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

Once you establish the virtual space, made it operational. Every time you want to work on the project, you need to make sure the appropriate virtual environment is running. To achieve this, enter the following command:

$ source virtualenv_name/bin/activate

After activating it, the terminal’s left side will display your virtual environment’s name. In other words, this means that the virtual machine is online and ready for use. The below image shows an operational venv named virtual environment. This virtual setting is now ready for you to install the necessary project dependencies. To give you an example, if you are using Django 1.9 for a project, you may install it just like any other package.

(virtualenv_name)$ pip install Django==1.9

The virtualenv name folder will house the Django 1.9 package, which will be completely cut off from the rest of the computer. When you finish using the virtual environment, you can turn it off using the following command.

(virtualenv_name)$ deactivate

Python Standard Library

For unit testing, interfacing with the operating system, handling dates and timings, and other tasks, the Python standard library comes with a number of helpful modules. However, you’ll frequently need to install additional packages when working on Python projects. In particular, web scraping for data collecting, machine learning, and online apps fall under this category. Use conda or pip to install and manage these packages. You must install a particular collection of packages depending on the project you are working on. However, all the projects share the globally installed packages when you install all the packages in your development environment on your local workstation.
Well, your development environment might contain N packages. However, the project you are currently working on might only need 3 of them. It is quite challenging to determine which projects required which installed packages—the dependencies connected to each project—when all of your projects share common installs. This strategy has still another drawback. Suppose your project library contains a Django 2.2 project. You choose to begin working on a Django 4 project. So, using the same development environment, you install Django’s most recent version.


In this article, we addressed the constraints of system-wide installations and how they make dependency management across Python projects problematic. Specifically, we focused on how these limitations make it more difficult to install packages globally. Python’s virtual environments offer a method for more effectively managing dependencies while also providing a development environment that is separate for each individual project.

You have learnt how to use venv, a tool included in the Python standard library so you can create and activate virtual environments. venv is one of the tools to construct and manage virtual environments in Python. It is possible to install versions of libraries that are unique to a project inside of the dedicated virtual environment that a project has. After that, and thanks to a file called “requirements.txt”, developers will be able to recreate the conditions of the project. When you begin the next Python project you’re working on, make sure to take use of virtual environments so you can more effectively handle dependencies.






Python and Excel Projects for practice
Register New Account
Shopping cart