Skip to content

How to Resolve Python "ModuleNotFoundError: No module named virtualenv"

Fix the Python error when the virtualenv package is missing by installing it with pip or switching to Python's built-in venv module for creating isolated environments.

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 the virtualenv command-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 using python -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 virtualenv package using pip or conda.
  • Incorrect Python Environment: virtualenv was 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 virtualenv globally 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 virtualenv installation.
  • PATH Issues: Less commonly, the directory containing the virtualenv executable isn't in your system's PATH environment variable (usually handled correctly by pip).

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
    
    pip3 install virtualenv
    
    python -m pip install virtualenv
    python3 -m pip install virtualenv
    py -m pip install virtualenv # Windows 'py' launcher
    
    pip install virtualenv --user
    sudo pip3 install virtualenv # Linux/macOS system-wide
  • Using conda (for Anaconda/Miniconda environments):

    conda install -c anaconda virtualenv

After installation, you should be able to use the virtualenv command or import virtualenv (if needed in a script).


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

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.


python3 -m venv venv

source venv/bin/activate

(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
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

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.

  1. To fix the error directly: Install the package using pip install virtualenv into the correct Python environment (preferably global or a dedicated tools environment, often not inside the project venv you intend to create).
  2. Recommended Alternative: For simply creating virtual environments in Python 3.3+, use the built-in venv module (python -m venv <env_name>). This avoids the need to install the virtualenv package at all.
  3. 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.

Navigation