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 --versionIf 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 python3Step 2: Create a Virtual Environment
To create a virtual environment, navigate to your project directory and run the following command:
python3 -m venv myenvThis 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/activateAfter 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\activateSimilarly, your command prompt will display the name of the active environment.
Step 4: Deactivate the Virtual Environment
To deactivate the virtual environment, simply run:
deactivateStep 5: Managing Dependencies
Once your virtual environment is active, you can install dependencies using pip. For example:
pip install requestsThis 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.txtTo install all dependencies listed in requirements.txt, run:
pip install -r requirements.txtStep 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/activateSave this script as activate_myenv.sh and make it executable:
chmod +x activate_myenv.shNow, you can activate your virtual environment by running:
./activate_myenv.shBest Practices
- Consistency: Use the same virtual environment across all development machines.
- Version Control: Commit your
requirements.txtto 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.Python Fundamentals Cheat Sheet for Scripting Beginners
- 3.Mastering Python Virtual Environments with python3 -m venv
7 more parts coming soon.

Written by Robin te Hofstee
Technology enthusiast with a deep love for programming. Sharing practical guides on React, Next.js, Linux and self-hosting.
Related Posts
How to Set Up a Python Web Server
A detailed tutorial on setting up a Python web server using Flask and deploying it locally.
Python Fundamentals Cheat Sheet for Scripting Beginners
A quick reference guide for essential Python scripting concepts, including data structures, control flow, and function definitions.
Setting Up Python Virtual Environments
Learn how to set up and manage Python virtual environments for your projects, ensuring isolated and reproducible development.


