Updated July 6, 2023
Introduction to Python Virtualenv
Python virtual environment of venv module provides developers the capability of creating a quite lightweight virtual environment with its standalone directory. This directory can be a different one as well from the system directory; that is, it can be optionally isolated as well. Each Python virtual environment can have its Python binary and can have its standalone Python packages as well installed in its site directory. If in case we have different projects to work on and require to keep their dependencies separately, then Python virtual environments can be used. The Python virtualenv makes it the most widely used feature in Python.
The syntax to create the virtual environment is mentioned down below.
python3 -m venv /path/to/new/virtual/environment
This command will not provide any output but will perform the below operations in the background:
- It creates a target directory.
- It also creates parent directories, even if they do not exist before
- It places a Python environment file inside the directory. The file is – pyvenv.cfg
- It also creates a subdirectory that contains Python binaries.
Why do We Need Virtual Environments in Python?
Consider a scenario wherein you are working on two different projects and one of them uses Django 1.9, and the other one requires Django1.10
In such cases, virtual environments work like a charm to maintain the dependencies of multiple projects. Alternate commands that can be invoked over Windows as well:
c:\>c:\Python35\python -m venv c:\path\to\myenv
c:\>python -m venv c:\path\to\myenv
How Does Virtual Environment Creation Work?
Here we discuss the working of virtual environment creation with Different Arguments Pass.
These arguments are order-specific when passed while calling a function.
- ENV_DIR: It is a positional argument that creates the directory n the environment we are working.
These arguments can be skipped as well. In a nutshell, as the name specifies, these are not compulsory ones.
- -h or –help: This optional argument displays the help message during the virtual env creation and exits.
- –system-site-packages: This optional argument provides access to the site-packages directory to the virtual env being created.
- –symlinks: This optional argument prefers using symlinks instead of using copies.
- –copies: This optional argument prefers using copies instead of using symlinks.
Let’s take an example to understand creating the env directory while creating the Python virtual env:
## Python program to understand how virtual env works in python def create(self, env_dir): ## Function to create a virtualized python directory ##Target directory is the env_dir env_dir = os.path.abspath(env_dir) context = self.ensure_directories(env_dir) self.create_configuration(context) self.setup_python(context) self.setup_scripts(context) self.post_setup(context)
Eventually, this program will not provide any output but will run several processes at the backend.
- ensure_directories(env_dir): This function eventually creates all of the required directories. This returns a context object as soon as this function is called
- The directories may exist previously as well
- create_configuration(context): This function will create a configuration file. The fine name is pyvenv.cfg
- setup_python(context): For the Python executable files residing in the environment, This function creates a copy of the symlink
- setup_scripts(context): There needs to be an activation script for each virtual environment, and this is what this function creates.
- post_setup(context): It comes up with a placeholder method to pre-install the packages.
How We Can Manage Python virtualenv?
The simplest way of reproducing the same work is to incorporate a requirements file inside the root directory of our project.
Let’s take an example to understand this in detail:
(venv) % pip freeze
To implement the same, we have run pip freeze:
- This indeed provides the list of all the third-party packages installed along with their version numbers
- The output of this command, that is, the list of all the third-party packages installed along with their version numbers, is written to a file called – requirements.txt
- Moreover, we can utilize the same command in order to rewrite the requirements file whensoever we install a new package or update the one
(venv) % pip freeze > requirements.txt
Now if we share this project and try to run this in another virtual environment, then we will be able to do the same with quite ease.
How We Can Duplicate Python Virtualenv?
Usually, we tend to exclude the venv/ folders, and that’s the reason we have the requirements.txt file as the major dependency to reproduce the code of your project in some other virtual environment.
To run a project created on one machine, on some other,
- All we need to do is create a virtual env inside the project root directory.
python3 -m venv venv/
- Then we need to install the project dependencies inside the active virtual env utilizing the below command:
pip install -r requirements.txt
This creates a replication of the project environment, which eventually makes us run the project on the other virtual env as well.
Activate the Virtual Environment
In order to activate the multiple virtual environments that you have created in for taking care of multiple projects, all you need to do is use the ‘activate’ command.
Deactivate the Virtual Environment
In order to deactivate the multiple virtual environments that you have created for taking care of multiple projects, all you need to do is use the ‘deactivate’ command.
It is quite useful to have more than one Python environment configured in order to test multiple libraries or to take care of multiple dependencies of different projects that you have been working on. This makes it the most widely used feature in Python.
- A virtual env is simply a copy of the original Python version installed on your machine with an option available to inherit the existing Python libraries/packages.
- Moreover, you can install variant libraries with even different versions of the same libraries/packages as per your needs.
- It’s quite useful when it comes to a shared system. As it provides each user the capability to create their own virtual env
- It indeed creates a folder that contains all of the necessary directories required to initiate the same.
This is a guide to Python Virtualenv. Here we discuss the introduction to Python virtualenv and why we need a virtual environment in Python, along with programming examples. You may also have a look at the following articles to learn more –