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.
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-envOutput:
/usr/bin/python3: No module named venvSolution 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:
sudo apt update
sudo apt install python3-venvAfter the installation is complete, you can verify that the venv module is now available.
python3 -m venv --helpOutput 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 virtualenvStep 2: Create the environment using the virtualenv command
virtualenv my-project-envThis 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:
python3 -m venv myenv2. Activate the Environment:
source myenv/bin/activate
myenv\Scripts\activate.bat
.\myenv\Scripts\Activate.ps1Your 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.
deactivateConclusion
| 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.