How to Resolve Python "ModuleNotFoundError: No module named 'virtualenv'"
The ModuleNotFoundError: No module named 'virtualenv' typically occurs in Python when you attempt to use the virtualenv command-line tool or, less commonly, import the virtualenv package directly in a script, but the package itself is not installed in the currently active Python environment. virtualenv is a popular third-party tool for creating isolated Python environments.
This guide explains the common causes of this error and provides solutions, including installation via pip and highlighting the built-in venv alternative.
Understanding the Error: virtualenv Package vs. venv Module
It's crucial to distinguish between:
virtualenv: A third-party package that you install (pip install virtualenv). It provides thevirtualenvcommand-line tool to create isolated Python environments. It was the standard tool before Python 3.3.venv: A built-in module included with Python 3.3 and later. It provides similar functionality for creating virtual environments directly usingpython -m venv <env_name>. It does not require a separate installation for basic use.
The ModuleNotFoundError: No module named 'virtualenv' specifically means the virtualenv package is missing or can not be found by the Python interpreter being used. It does not relate directly to the built-in venv module unless venv itself failed to install pip or setuptools correctly within the created environment (which is rare).
Common Causes of the Error
- Package Not Installed: You haven't installed the
virtualenvpackage usingpiporconda. - Incorrect Python Environment:
virtualenvwas installed for a different Python installation or virtual environment than the one currently active in your terminal or IDE. - Virtual Environment Not Activated: You installed
virtualenvglobally but are trying to use it from within an unactivated virtual environment that doesn't have it, or vice-versa. - IDE Misconfiguration: Your IDE is using a Python interpreter that lacks the
virtualenvinstallation. - PATH Issues: Less commonly, the directory containing the
virtualenvexecutable isn't in your system's PATH environment variable (usually handled correctly bypip).
Solution 1: Install the virtualenv Package (pip/conda)
If you intend to use the virtualenv tool or package, you need to install it into the relevant Python environment. Open your terminal or command prompt, activate the desired virtual environment if applicable, and run:
-
Using
pip(Standard):pip install virtualenv
# Or use pip3 if needed
pip3 install virtualenv
# Or use python -m pip if pip is not directly in PATH
python -m pip install virtualenv
python3 -m pip install virtualenv
py -m pip install virtualenv # Windows 'py' launcher
# If permission errors occur (less common when using virtual envs):
pip install virtualenv --user
# Or (use with caution for global installs):
sudo pip3 install virtualenv # Linux/macOS system-wide -
Using
conda(for Anaconda/Miniconda environments):# Activate your conda environment first: conda activate your_env_name
conda install -c anaconda virtualenv
# Or often available from conda-forge:
# conda install -c conda-forge virtualenv
After installation, you should be able to use the virtualenv command or import virtualenv (if needed in a script).
# Example: Using the command after installation
# (Run this in your terminal, not as Python code)
# virtualenv my_new_env_using_virtualenv
# Example: Importing after installation
try:
import virtualenv
print("Successfully imported virtualenv package.")
print(f"Location: {virtualenv.__file__}")
except ModuleNotFoundError:
print("ERROR: virtualenv still not found after installation attempt.")
except Exception as e:
print(f"An error occurred: {e}")
Solution 2: Verify the Python Environment
Ensure consistency between where you install and where you run virtualenv.
Checking Python Version
Verify the Python version associated with the pip command you used.
python --version
pip --version
# Or: python3 --version / pip3 --version
If you have multiple Pythons, ensure you installed virtualenv using the pip corresponding to the python you intend to use it with.
Activating the Correct Virtual Environment
This is critical. If you want to use virtualenv from within an existing virtual environment (less common, usually you use it globally to create environments), make sure that environment is activated first (source venv/bin/activate, etc.) before installing or running virtualenv. More commonly, you install virtualenv globally or in a base environment and use it to create new, separate environments.
Checking IDE Interpreter Settings
If running commands or scripts from within an IDE, ensure the IDE's configured Python interpreter is the one where virtualenv was installed (or that it's correctly configured to find global packages if you installed it globally).
Solution 3: Use the Built-in venv Module (Alternative Approach)
For creating virtual environments in Python 3.3+, using the built-in venv module is the standard and recommended approach. It does not require installing the virtualenv package. If your goal is simply to create a virtual environment, switching to venv avoids the need for the virtualenv package entirely, thus sidestepping the ModuleNotFoundError.
# How to create an environment using the built-in venv module
# (Run this in your terminal)
# 1. Choose your Python executable (python, python3, py)
# 2. Use the '-m venv' flag followed by the desired environment name (e.g., 'venv')
python3 -m venv venv
# Or: python -m venv venv
# Or: py -m venv venv (Windows)
# 3. Activate the new environment (OS-dependent commands)
# Linux/macOS:
source venv/bin/activate
# Windows CMD:
# venv\Scripts\activate.bat
# Windows PowerShell:
# venv\Scripts\Activate.ps1
# 4. (venv) prompt appears. Now install project packages using pip:
(venv) pip install requests Flask ...
Using venv is generally preferred unless you specifically need features provided only by the third-party virtualenv package.
Debugging Steps
If you installed virtualenv but still get the error:
Check if virtualenv is Installed (pip show)
In your activated terminal/environment where you expect it to be:
pip show virtualenv
# Or:
python -m pip show virtualenv
Check if found and verify the Location:.
Restart Terminal / IDE
Close and reopen your terminal, command prompt, or IDE to ensure PATH changes or environment activations are fully registered.
Reinstall / Upgrade virtualenv
pip uninstall virtualenv
pip install virtualenv
# Or upgrade:
pip install --upgrade virtualenv
Conclusion
The ModuleNotFoundError: No module named 'virtualenv' means the third-party virtualenv package is not installed or accessible in your current Python path.
- To fix the error directly: Install the package using
pip install virtualenvinto the correct Python environment (preferably global or a dedicated tools environment, often not inside the project venv you intend to create). - Recommended Alternative: For simply creating virtual environments in Python 3.3+, use the built-in
venvmodule (python -m venv <env_name>). This avoids the need to install thevirtualenvpackage at all. - Always verify your active Python environment (terminal prompt, IDE settings) to ensure consistency between where packages are installed and where commands/scripts are run.
Choose the approach that fits your needs: install virtualenv if you specifically require that tool, or use the standard venv module for creating environments.