Nearby lessons
156 of 159Python - Virtual Environment
- Understand what a virtual environment is
- Know why virtual environments are needed
- Create a virtual environment using venv
- Activate and deactivate a virtual environment
- Install packages inside an isolated environment
What is a Virtual Environment?
A virtual environment is an isolated Python environment created for a specific project.
It has its own copy of Python, its own pip, and its own set of installed packages. Packages installed inside one virtual environment do not affect other environments or the system Python.
Why Do We Need Virtual Environments?
Consider two projects in which we work. One project needs requests version 2.31.0, and the other needs version 2.20.0.
If both projects share the same system Python, installing one version would break the other project.
A virtual environment solves this problem by giving each project its own isolated set of packages.
The venv Module
Python 3 provides the venv module, which is built into the standard library, to create virtual environments.
To create a virtual environment, open a command prompt in the project folder and run the following command:
Explanation
python -m venvruns the venv module as a script.myenvis the name of the folder that will contain the new environment.
After running the command, a folder named myenv is created. It contains the environment's own Python executable, pip, and an empty site-packages directory.
Activating the Virtual Environment
Before installing packages, the virtual environment must be activated. The activation command depends on the operating system.
Explanation
After activation, the environment name appears at the beginning of the command prompt, like this:
Installing Packages Inside the Environment
Once activated, any pip install command installs the package into the virtual environment, not the system Python.
Verifying the Installation Location
We can confirm that a package was installed inside the environment by checking the version of pip from within it.
Deactivating the Virtual Environment
When we finish working, we can leave the virtual environment using the deactivate command.
Explanation
After deactivation, the environment name disappears from the command prompt, and commands again refer to the system Python.
Removing a Virtual Environment
A virtual environment is just a folder. To remove it, delete the folder after deactivating it.
System Python vs Virtual Environment
| System Python | Virtual Environment |
|---|---|
| Shared by all projects. | Created separately for each project. |
| Installing a package affects all projects. | Installing a package affects only that environment. |
| Version conflicts are possible. | Version conflicts are avoided. |
| No activation needed. | Must be activated before use. |
Benefits of Virtual Environments
- Each project can have its own package versions.
- Conflicts between projects are avoided.
- Dependencies can be installed without administrative permissions.
- An environment can be recreated easily from a
requirements.txtfile. - The system Python installation stays clean.
- A virtual environment is an isolated Python environment for a project
- It prevents package version conflicts between different projects
- python -m venv myenv creates a new virtual environment
- The environment must be activated before installing packages into it
- Each environment has its own copy of pip and its own site-packages