The venv module supports creating lightweight “virtual environments” with their own independent set of Python packages installed in their site directories.

1. Creating a virtual environment

cd project-path/
python -m venv <name-of-virtual-envrionment>

Replace <name-of-virtual-environment> with the name of choice, a convention is usually venv, my-env, virtual-env

2. Activate the virtual environment

To activate the virtual environment, from the current project directory, in our case(project-path)

cd project-path/
# create the virtual environment with the name "my-venv"
python -m my-venv 
# activate the virtual environment
source venv/bin/activate

Running this command creates the target directory and places pyvenv.cfg file in the home key which the command was run.

Read more about venv from the official docs

Most Python projects require a “sandbox” virtual environment. Modern code editors and IDEs provide great support for virtual environments created with venv

Found this article helpful? You may follow me on Twitter where I tweet about interesting topics on software development.