How to create and activate a virtual environment

Posted in Python by Dirk - last update: Feb 06, 2024

What is an virtual environment

A virtual environment in Python is a self-contained directory that houses a specific Python interpreter along with its own set of installed packages. The purpose of using virtual environments is to isolate Python projects, ensuring that dependencies for one project do not interfere with those of another. This helps manage project-specific dependencies and versions, making it easier to maintain and share code across different projects.

How Do I Create a Virtual Environment in Python?

You can create a virtual environment using the venv module

python -m venv my_env

Replace my_env with the desired name for your virtual environment.

The built-in venv module was introduced in Python 3.3 and later versions as a part of the standard library to provide a simple and convenient way to create isolated Python environments.

What happens when you create a virtual environment with python -m venv:

1. Creation of Directory Structure: a new directory is created with the name you provide (e.g., my_env). The directory contains subdirectories like bin (or Scripts on Windows), include, and lib.

2. Isolation of Python Interpreter: the virtual environment includes a copy of the Python interpreter from the global environment. This ensures that the virtual environment has its own Python executable, separate from the system-wide Python installation.

3. Installation of Standard Library: the virtual environment includes a copy of the Python standard library.

4. Installation of pip: the venv module ensures that the virtual environment has its own copy of the pip package manager.

5. Activation Script: a script (activate on both Windows and macOS/Linux) is created inside the bin (macOs/Linux) or Scripts (Windows) directory. Running this script activates the virtual environment.

6. Deactivation Script: a script (deactivate on all platforms) is also provided to deactivate the virtual environment.

7. Isolation of site-packages: the site-packages directory inside the virtual environment’s lib directory is empty initially. When you install packages within the virtual environment using pip, they are added to this directory, ensuring isolation from global packages.

How Do I Activate a Virtual Environment in Python?

Activating a virtual environment in Python is straightforward, but depends on the platform.

Activate a Virtual Environment on Linux/MacOs

You can do this via the command source my_venv/bin/activate. This uses the ‘activate’ script located in the ‘Scripts’ directory of your virtual environment (my_venv).

Here’s a simple example :

source my_venv/bin/activate

Activate a Virtual Environment on Windows

Cmd: execute the activate.bat file in the Scripts directory of your virtual environment.

C:\>	<my_venv>\Scripts\activate.bat

Powershell: execute the Activate.ps1 file in the Scripts directory of your virtual environment.

C:\>	<my_venv>\Scripts\Activate.ps1

Install Packages

With the virtual environment activated, you can use pip to install packages, and they will be isolated to that specific environment.

pip install package_name

Deactivate the Virtual Environment

To deactivate the virtual environment and return to the global Python environment, use the following command:

deactivate

Use a requirements.txt

Using a requirements.txt file is a good practice when working with Python virtual environments. A requirements.txt file helps you document and manage the dependencies for your Python project. It typically lists the names and versions of Python packages required for a specific project to run correctly. This file is useful for managing and reproducing the exact environment of a project by installing the specified dependencies.

  • Reproducibility: by specifying the exact versions of the packages your project depends on, you ensure that your project can be reproduced in the same state on different machines or by different developers. This helps avoid compatibility issues and ensures consistent behavior.
  • Isolation: virtual environments are created to isolate Python projects, but they are not inherently self-documenting in terms of dependencies. The requirements.txt file serves as a clear and standardized way to document and communicate the project’s dependencies.
  • Sharing and Collaboration: when you share your project with others, whether it’s a colleague or a deployment system, providing a requirements.txt file makes it easy for them to set up the required environment. It’s a common convention used in the Python community, making it familiar to other developers.
  • Automated Dependency Management: continuous integration tools and deployment platforms often use requirements.txt to install project dependencies automatically. This simplifies the process of deploying your application to different environments.
  • Upgrading Dependencies: when you need to update your project’s dependencies, you can modify the requirements.txt file and then easily update the virtual environment using pip install -r requirements.txt --upgrade.

How to create a requirements.txt:

Manually: you can create the file yourself using a text editor and list the dependencies one per line. For example

requests==2.26.0
Flask==2.0.1

Automatically: you can use the pip freeze command to generate a requirements.txt file from the currently installed packages in your virtual environment. Run the following command in your terminal with the virtual environment activated:

pip freeze > requirements.txt

Installing dependencies from a requirements.txt

To install the dependencies listed in the requirements.txt file, you can use the following command:

pip install -r requirements.txt --upgrade

This command reads the file and installs the specified versions of the packages into your Python environment.

Include the requirements.txt file in your project’s version control system (e.g., Git) so that others can easily recreate the environment.

Other articles