Setup a Python Virtual Env

A Python virtual environment is a self-contained directory or folder that contains all the files needed for a specific Python project.

A virtual environment allows you to have multiple Python projects with different dependencies on the same machine without interfering with each other. It does this by creating an isolated environment with its own Python interpreter, packages, and modules, separate from the system’s Python installation.

By using virtual environments, you can manage the dependencies of each project independently, install different versions of packages for different projects, and avoid version conflicts or compatibility issues between different projects.

Python virtual environments are created and managed using tools such as venv, virtualenv, and conda, and are a standard practice in Python development.

How to install a virtual environment using Venv onto Windows

Install Venv onto your Python workspace

PS ~ pip install virtualenv

Collecting virtualenv
  Downloading virtualenv-20.21.0-py3-none-any.whl (8.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.7/8.7 MB 19.9 MB/s eta 0:00:00
Collecting distlib<1,>=0.3.6
  Downloading distlib-0.3.6-py2.py3-none-any.whl (468 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 468.5/468.5 kB 9.8 MB/s eta 0:00:00
Collecting filelock<4,>=3.4.1
  Downloading filelock-3.12.0-py3-none-any.whl (10 kB)
Collecting platformdirs<4,>=2.4
  Downloading platformdirs-3.2.0-py3-none-any.whl (14 kB)
Installing collected packages: distlib, platformdirs, filelock, virtualenv
Successfully installed distlib-0.3.6 filelock-3.12.0 platformdirs-3.2.0 virtualenv-20.21.0

[notice] A new release of pip available: 22.3.1 -> 23.1
[notice] To update, run: python.exe -m pip install --upgrade pip
PS ~

To use venv in your project, in your terminal, create a new project folder, cd to the project folder in your terminal, and run the following command:

PS ~ mkdir vnenv-project00

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          19/04/2023    14.55                vnenv-project00

PS ~ cd .\vnenv-project00\
PS ~\vnenv-project00> python3.11 -m venv myenv
PS ~\vnenv-project00>

In the myenv folder will be the created Venv data.

PS ~\vnenv-project00\myenv> ls

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          19/04/2023    15.02                Include
d----          19/04/2023    15.02                Lib
d----          19/04/2023    15.02                Scripts
-a---          19/04/2023    15.02            376 pyvenv.cfg

To active the virtual environment run this command. Notice that when activated your prompt will have (your_env) prefixed.

PS ~\vnenv-project00\myenv> cd scripts
PS ~\vnenv-project00\myenv\Scripts> ls

    Directory: C:\Users\neilg_fe5udmm\github\grinntec\python\vnenv-project00\myenv\Scripts

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          19/04/2023    15.02           2109 activate
-a---          19/04/2023    15.02           1038 activate.bat
-a---          19/04/2023    15.02          26195 Activate.ps1
-a---          19/04/2023    15.02            393 deactivate.bat
-a---          19/04/2023    15.02         108445 pip.exe
-a---          19/04/2023    15.02         108445 pip3.11.exe
-a---          19/04/2023    15.02         108445 pip3.exe
-a---          19/04/2023    15.02         270616 python.exe
-a---          19/04/2023    15.02         259344 pythonw.exe

PS ~\vnenv-project00\myenv\Scripts> .\Activate.ps1
(myenv) PS ~\vnenv-project00\myenv\Scripts>

To deactive the virtual environment simply run deactivate in the shell.

References

How to Set Up a Virtual Environment in Python

Venv

Last modified July 21, 2024: update (e2ae86c)