How to Resolve "Error: No module named venv" in Python
When trying to create a Python virtual environment, you might encounter the Error: No module named venv. This error indicates that the Python interpreter you are using was installed without the standard venv library. While venv has been part of Python's standard library since version 3.3, some operating system distributions (most notably Debian and Ubuntu) package it separately to keep the core Python installation minimal.
The solution is to install the specific system package that provides the venv module for your Python installation. This guide will show you how to fix this for Debian-based systems and explain the difference between the built-in venv and its predecessor, virtualenv.
Understanding the Error: A Separately Packaged Standard Library
The venv module is the standard, built-in tool for creating isolated Python virtual environments. However, when you install Python on a system like Ubuntu, the package manager often splits the Python installation into several packages. The core interpreter is in one package (e.g., python3.10), while other parts of the standard library, like venv, are in optional packages (e.g., python3.10-venv).
The error message No module named venv means this optional package was never installed.
venv vs. virtualenv
venv: The built-in module for creating virtual environments, available since Python 3.3. This is the module your interpreter is looking for.virtualenv: A third-party package that was the standard beforevenvwas added to Python. It is more powerful and supports older Python versions.
Installing virtualenv with pip will not fix the No module named venv error, but it provides a powerful alternative for creating environments.
Reproducing the ModuleNotFoundError
On a minimal Debian or Ubuntu installation, running the standard command to create a virtual environment will trigger the error.
Example of command causing the error:
python3 -m venv my-project-env
Output:
/usr/bin/python3: No module named venv
Solution 1: Install the python3-venv System Package (Debian/Ubuntu)
The correct and most direct solution is to install the missing system package using apt.
Solution:
# First, update your package list
sudo apt update
# Then, install the venv package for your Python 3 version
sudo apt install python3-venv
After the installation is complete, you can verify that the venv module is now available.
python3 -m venv --help
Output will show the usage instructions, confirming success.
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
[--upgrade] [--without-pip] [--prompt PROMPT] [--upgrade-deps]
VENV_DIR [VENV_DIR ...]
Solution 2: Use virtualenv as an Alternative
If you do not have sudo permissions to install system packages, or if you prefer the features of virtualenv, you can install and use it as a powerful alternative.
Step 1: Install virtualenv using pip
pip install virtualenv
Step 2: Create the environment using the virtualenv command
virtualenv my-project-env
This will create a virtual environment in the my-project-env directory, which you can activate just like a venv environment.
How to Create and Use a Virtual Environment (Once Fixed)
Once you have resolved the error by installing python3-venv, you can create and manage your virtual environments with the following standard commands.
1. Create the Environment:
# This creates a new directory named 'myenv' with the virtual environment
python3 -m venv myenv
2. Activate the Environment:
# On Linux or macOS
source myenv/bin/activate
# On Windows (Command Prompt)
myenv\Scripts\activate.bat
# On Windows (PowerShell)
.\myenv\Scripts\Activate.ps1
Your terminal prompt will change to indicate that the environment is active (e.g., (myenv) $).
3. Deactivate the Environment:
When you are finished, simply run the deactivate command.
deactivate
Conclusion
| Underlying Problem | Recommended Solution |
|---|---|
The venv standard library module is missing on Debian/Ubuntu. | Install the system package: sudo apt install python3-venv. |
| You cannot install system packages or prefer a more powerful tool. | Install and use the third-party virtualenv package: pip install virtualenv. |
The No module named venv error is a system configuration issue, not a problem with Python itself. By installing the appropriate package for your OS, you can restore this essential functionality for managing isolated Python projects.