No Module Named Venv in Python 3

The venv module comes pre-installed in Python 3.3 and later. If the module is not installed, then you will run into ModuleNotFoundError: No module named ‘venv’ when you try importing the module on the script (by running “import venv“) or on the terminal/command line when you run “python -m venv <env_name>“.

What is venv?

The venv module is used to create a virtual environment in Python 3. A virtual environment allows one to isolate Python packages from the system-wide packages and modules in other environments. Each virtual environment has its own Python binary/executable (which matches the version of the binary/executable used to create this environment).

Later, we will see how to create a virtual environment using venv but for now, let’s discuss how to solve ModuleNotFoundError: No module named ‘venv’.

Solution 1: Install the venv module using a package manager

In Linux, you can install the module using APT (or any other package manager for your distro) by running the following two commands in the terminal.

Once that is done, you can now call python3-venv with venv shorthand.

Solution 2: Use the virtualenv module – an alternative

Like venv, the virtualenv module creates virtual environments in Python. The only difference in their functionality is that the virtualenv module is available in older versions of Python (earlier than Python 3.3 and even Python 2) and has a few more minor unique features. The virtualenv library can be installed using pip by running

We will learn how to use virtualenv to create a virtual environment in Python later in this article.

Solution 3: Install the module manually from the GitHub

The official documentation of the venv module, located at https://docs.python.org/3/library/venv.html contains the link to the source code of the module. The source code is found at the following link: https://github.com/python/cpython/tree/main/Lib/venv during the time of writing.

The link leads us to a folder within the main repo –cpython. We don’t want the entire cpython repo (so we won’t clone it. It is so large anyway.), we only need the venv folder within Lib.

We can download a folder within a GitHub repo using the site: https://downgit.github.io/ and the module url, that is, https://github.com/python/cpython/tree/main/Lib/venv.

Once the zipped file (venv.zip) is downloaded, we need to unzip it and copy the resulting folder into the location containing standard Python libraries.

We can locate the directory containing Python libraries by running the following command in the terminal.

Output (In my case):

['', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/home/kiprono/.local/lib/python3.9/site-packages', '/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages']

Note: Python 3.9 is my default Python version. If you want to use another installed Python version, say 3.10, then you run python3.10 -c ‘import sys; print(sys.path)’ or provide the full path to the binary.

The standard libraries are in “/usr/lib/python3.9“. Depending on the Python version installed, you will get something like “/usr/lib/python3.**“. If you are running windows, the libraries will be located in “C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.***\Lib“. Next, we can then copy the venv directory into the folder we just discovered above (we need sudo privileges to do this).

sudo cp -r path/to/venv/folder path/to/folder/with/libraries

for example,

Make sure you copy the directory with its content; that is, the src path should end with venv, not venv/.

Once that is done, Python should detect the module.

Note: we can install the module manually without breaking anything because venv is not linked to other packages.

Creating and using a virtual environment with venv or virtualenv

You can create a virtual environment by running any of the following commands on the terminal

For virtual env,

General syntax: virtualenv -p <path/to/python/binary> </location/of/env/name>

Example:

This will create a virtual environment called venv3 at the current working directory. The python3 command is a short hand pointing to the actual location of the Python binary

For the venv module,

General syntax: <path/to/python/binary> -m venv </location/of/env/name>

For example,

Activating the virtual environment

The command for activating the created environment does not vary based on the module used to make it but instead on the OS and the shell you are using.

  • On Unix or macOS, using the bash shell: source /path/to/venv/bin/activate
  • On Unix or macOS, use the csh shell: source /path/to/venv/bin/activate.csh
  • On Unix or macOS, use the fish shell: source /path/to/venv/bin/activate.fish
  • On Windows, use the Command Prompt: path\to\venv\Scripts\activate.bat
  • On Windows, using PowerShell: path\to\venv\Scripts\Activate.ps1

(By default, Windows PowerShell may not allow you to run scripts. You can change that by starting PowerShell as admin and executing “Set-ExecutionPolicy RemoteSigned” then; you will be able to start the virtual environment even as a standard user).

Note: Once the environment is activated, the environment name will precede the terminal prompt ($).

To deactivate the environment, run:

Conclusion

The venv module is in-built into Python 3.3+, but if you are running an older version of Python 3, you can use the three suggestions provided in this article.