Nearby lessons

156 of 159

Python - Virtual Environment

📌 What You Will Learn
  • 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:

🐍Code Cell
1python -m venv myenv
Output
No output captured.

Explanation

  • python -m venv runs the venv module as a script.
  • myenv is 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.

🐍Code Cell
1# On Windows
2myenv\Scripts\activate
3 
4# On macOS / Linux
5source myenv/bin/activate
Output
No output captured.

Explanation

After activation, the environment name appears at the beginning of the command prompt, like this:

🐍Code Cell
1(myenv) C:\Users\name\myproject>
Output
No output captured.

Installing Packages Inside the Environment

Once activated, any pip install command installs the package into the virtual environment, not the system Python.

🐍Code Cell
1pip install requests
Output
Collecting requests
  Downloading requests-2.31.0 ...
Successfully installed requests-2.31.0

Verifying the Installation Location

We can confirm that a package was installed inside the environment by checking the version of pip from within it.

🐍Code Cell
1where python
Output
C:\Users\name\myproject\myenv\Scripts\python.exe

Deactivating the Virtual Environment

When we finish working, we can leave the virtual environment using the deactivate command.

🐍Code Cell
1deactivate
Output
No output captured.

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.

🐍Code Cell
1# On Windows
2rmdir /s myenv
3 
4# On macOS / Linux
5rm -rf myenv
Output
No output captured.

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.txt file.
  • The system Python installation stays clean.
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

10 Questions
Progress: 0 / 10