What Is "venv" in Python?

Introduced in Python 3.3, venv is a built-in Python module that allows you to create a virtual environment for your projects. This can be great for working in an isolated environment without impacting the system-wide Python installation (or other virtual environments).

You can create a virtual environment using venv by navigating to your project directory, and running a command like following in terminal:

python -m venv myenv

This will create a virtual environment named "myenv" in your project directory. Once created, you need to activate the environment to be able to install packages and work within that isolated environment.

To activate a virtual environment on Windows, you can run the following command:

myenv\Scripts\activate.bat

To activate a virtual environment on macOS and Linux, you can run the following command:

source myenv/bin/activate

Once activated, the shell's prompt will change to reflect the virtual environment, allowing you to keep your project's dependencies and workflow separate from the system-wide Python installation.

To deactivate the virtual environment and return to the system-wide Python installation, you can simply run the deactivate command in shell:

deactivate

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.