Robin.te.Hofstee

Mastering Python Virtual Environments with python3 -m venv

Robin te Hofstee4 min readViews2
Mastering Python Virtual Environments with python3 -m venv

Learn how to use python3 -m venv to create, activate, and manage virtual environments, ensuring clean and isolated development spaces.

Mastering Python Virtual Environments with python3 -m venv

Why Use Virtual Environments?

When developing Python applications, it's crucial to maintain a clean and isolated environment to avoid conflicts between dependencies. Global package pollution can lead to version mismatches, broken builds, and difficult-to-diagnose bugs. Python virtual environments provide a solution by creating a sandboxed environment where you can install and manage dependencies without affecting the global Python installation.

Setting Up Your Virtual Environment

Step 1: Install Python

First, ensure that Python 3 is installed on your system. You can check this by running:

python3 --version

If Python 3 is not installed, you can install it using your package manager. For example, on Ubuntu, you can use:

sudo apt update
sudo apt install python3

Step 2: Create a Virtual Environment

To create a virtual environment, navigate to your project directory and run the following command:

python3 -m venv myenv

This command creates a new directory named myenv in the current directory, which will contain the virtual environment.

Step 3: Activate the Virtual Environment

Activation of the virtual environment varies depending on your shell. Below are instructions for both bash and cmd.

Bash (Bash, Zsh, Fish)

To activate the virtual environment in bash, zsh, or fish, use:

source myenv/bin/activate

After activation, your shell prompt will change to show the name of the activated environment.

Cmd (Windows)

To activate the virtual environment in cmd, use:

myenv\Scripts\activate

Similarly, your command prompt will display the name of the active environment.

Step 4: Deactivate the Virtual Environment

To deactivate the virtual environment, simply run:

deactivate

Step 5: Managing Dependencies

Once your virtual environment is active, you can install dependencies using pip. For example:

pip install requests

This command installs the requests library within the virtual environment, isolated from the global Python installation.

Step 6: Creating and Managing Requirements Files

To save a list of installed packages, create a requirements.txt file:

pip freeze > requirements.txt

To install all dependencies listed in requirements.txt, run:

pip install -r requirements.txt

Step 7: Automating Activation Workflows

For seamless development, you can automate the activation process. Create a shell script for this purpose. Here’s an example for bash:

#!/bin/bash
source myenv/bin/activate

Save this script as activate_myenv.sh and make it executable:

chmod +x activate_myenv.sh

Now, you can activate your virtual environment by running:

./activate_myenv.sh

Best Practices

  • Consistency: Use the same virtual environment across all development machines.
  • Version Control: Commit your requirements.txt to your version control system to ensure others can replicate the environment.
  • Documentation: Document the steps to create and activate your virtual environment in your project’s README.

Conclusion

Mastering Python virtual environments is essential for maintaining a clean and isolated development space. By using python3 -m venv, you can avoid global package pollution, ensuring that your project dependencies are consistent and manageable. This practice not only enhances the quality of your code but also speeds up development by reducing the likelihood of version conflicts.

This article is part of a series on Python scripting. Stay tuned for more guides on advanced Python topics and best practices.

Tutorial series

Python Scripting Series

A step-by-step Python series for self-hosters and developers: from your first script and a clean environment to databases and local AI APIs.

  1. 1.Python Fundamentals Cheat Sheet for Scripting Beginners
  2. 3.Mastering Python Virtual Environments with python3 -m venv

7 more parts coming soon.

Share this post
Robin te Hofstee

Written by Robin te Hofstee

Technology enthusiast with a deep love for programming. Sharing practical guides on React, Next.js, Linux and self-hosting.