SoFunction
Updated on 2025-03-01

Description of the difference between virtual environment and venv and virtualenv

1. Overview of virtual environment

Python applications usually use packages and modules that are not in the standard library. Applications sometimes require specific versions of the library, fix specific bugs, or can write applications using an outdated version of the library's interface.

This means that a Python installation may not meet the requirements of each application. For example: Application A requires version 1.0 of a specific module but application B requires version 2.0, so there is a conflict in requirements. Installing version 1.0 or 2.0 will cause a certain application to fail to run.

The solution to this problem is to create a virtual environment, a directory tree, where specific Python versions are installed, and other packages.

Different applications will be able to use different virtual environments. This can resolve conflicts of demands.

2. Create and activate virtual environment

The module used to create and manage virtual environments is called venv. Venv will usually install the latest version of Python that you can use. If you have multiple versions of Python on your system, you can specify the Python version by running python3 or any version you want.

1. Create a virtual environment

If the project is called tutorial, create the corresponding folder and switch to the root directory: cd tutorial.

If using venv, use the command: $python -m venv tutorial-env

Tutorial-env is the name of the virtual environment, and it can be modified freely as the name of the created virtual environment folder. If the tutorial-env directory does not exist, it will create one for you and create a directory in it that contains copies of the Python interpreter, standard libraries, and various supporting files.

Note: The virtual environment folder name of tutorial-env needs to be added to the .gitignore file for Git to ignore.

The common directory location for virtual environments is .venv. This name will usually keep the directory hidden in your terminal, avoiding the general name that requires additional explanation of the directory you are in. It also prevents conflicts with the .env environment variable definition files supported by some tools.

If you use virtualenv, use the following command: $virtualenv snow-venv

2. Activate the virtual environment

Activating the virtual environment by executing the corresponding activation script, the activation commands (activation scripts and paths) of different operating systems are slightly different.

Windows():$ tutorial-env\Scripts\

Linux and macOS (bash/zsh): $ source tutorial-env/bin/activate

This script is written for bash shell. If you use csh or fish shell, you should use _ or script instead.

After activating the virtual environment, the name of the current virtual environment will be displayed before the command line prompt, and the environment will be modified so that the python command will run the specific Python version that has been installed.

Exit the virtual environment: deactivate

3. Venv module and virtualenv tools

virtualenv: Python virtual environment management tool.

venv: A virtual environment management tool built in the Python standard library, added by Python 3.3, and Python 3.5 has begun as a recommended tool for managing virtual environments. The usage is similar to virtualenv. The only difference is the way to create a virtual environment.

When creating a virtual environment, you need to install a third-party virtualenv to create a virtual environment. However, after Python 3.3, the standard library has built-in venv module, which can be used to create a virtual environment.

If you use Python 3.3 and above, it is recommended to use the built-in venv module in the standard library instead of virtualenv.

If you use Python 2, you can only choose virtualenv, and you need to install it extra. pip install virtualenv

Supplement: [python virtual environment] Is virtualenv different from venv

When developing python applications, there is only one version of python3 installed on the system: 3.4. All third-party packages are back pip installed in the site-packages directory of python3.

If we want to develop multiple applications at the same time, then these applications share a python, which is python3 installed on the system. What if application A requires jinja 2.7, and application B requires jinja 2.6?

In this case, each application needs to have a "independent" Python running environment. There are two common tools for creating python "standalone" environments:

venv is available by default in Python 3.3 and later, and installs pip and setuptools into created virtual environments in Python 3.4 and later.

virtualenv needs to be installed separately, but supports Python 2.7+ and Python 3.3+, and pip, setuptools and wheel are always installed into created virtual environments by default (regardless of Python version).

virtualenv

Virtualenv is used to create an "isolated" python running environment for an application.

First, we use pip to install virtualenv

pip3 install virtualenv

Then, suppose we want to develop a new project and need a separate Python running environment, we can do this:

In the project directory, create an independent python running environment named venv

virtualenv --no-site-packages venv

The command virtualenv can create an independent python running environment. We also added the parameter --no-site-packages, so that all third-party packages that have been installed in the system python environment will not be copied. In this way, we get a "clean" pyhton running environment without any third-party packages.

The newly created python environment is placed in the venv directory in the project directory. With the venv python environment, you can use source to enter the environment:

source ./venv/bin/activate

Or

source ./venv/Scripts/activate

Pay attention to check which directory the activate command is placed.

After executing the above command, you can understand that the command indicator has changed. There is a (venv) prefix, indicating that the current environment is a python environment called venv. Various third-party packages can be installed normally below and python commands can be run.

In the venv environment, all packages installed with pip are installed in the venv environment, the system python environment is not affected at all, that is, the venv environment is specially created for the current project.

Exit the venv environment and use the deactivate command:

deactivate

This will return to the normal environment.

How does virtualenv create a "standalone" python runtime environment? In fact, it is to copy the system python to the virtualenv environment. When using the command source to enter a virtualenv environment, virtualenv will modify the relevant link variables, so that the commands python and pip both point to the current virtualenv environment.

venv

Venv does not need to be installed, but it requires python3.3 or above. Create a standalone python runtime environment named venv

python3 -m venv ./venv

The above command will create a python environment called venv in the current directory. The way to enter and exit the environment is the same as that of virtualenv.

Reference documentation:Official Documentation

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.