How to fix the No module named venv error in Python

When attempting to create a virtual environment using venv, you might encounter this error:

/usr/bin/python: No module named venv

This error occurs when you don’t have the virtual environment module installed in your Python environment.

Note that the venv module was bundled starting from Python version 3.3, so if you’re using an older Python version, you need to install the module first.

To install venv, you need to run one of the following commands:

python -m pip install --user virtualenv

# For python 3:
python -m pip install --user virtualenv

# For Windows:
py -m pip install virtualenv

If you’re using Ubuntu, you may need to run the following commands to get pip and venv installed:

sudo apt-get install python3-pip
sudo apt-get install python3-venv

After you installed the module, you can check if the module is available by running one of the following commands:

python -m venv -h

python3 -m venv -h

py -m venv -h

If you get a help description, that means the venv module is now available.

You can create a new virtual environment by running the command below:

python -m venv <environment directory>

Replace <environment directory> with the name of the directory you want to create the virtual environment.

For example, this command will create one virtual environment named demoenv:

python -m venv demoenv

To activate the virtual environment, you need to run one of the following commands:

# activate on Unix or macOS:
source demoenv/bin/activate

# activate on Windows:
demoenv\Scripts\activate

demoenv\Scripts\activate.bat

demoenv\Scripts\activate.ps1

For Windows, the activation script has several alternatives. You can use the ps1 extension when you’re using PowerShell, or the usual bash command in command prompt and bash.

To deactivate the virtual environment, simply run deactivate command from the console.

I hope this tutorial helps you to solve the No module named venv error. Until next time!

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.