Python Virtual Environments

I want to read about Python virtual environments because I have been dealing with them recently.

Date Created:

References



Notes


The venev module supports creating lightweight "virtual environments", each with their own independent set of Python packages installed in their site directories. A virtual environment is created on top of an existing Python installation, known as the virtual environment's "base" Python, and may optionally be isolated from the packages in the base environment, so only those explicitly installed in the virtual environment are available.

A python virtual environment is:

  • Used to contain a specific Python interpreter and software libraries and binaries which are needed to support a project.
  • Contain a directory, conventionally names .venv or venv in the project directory
  • Not checked in source control systems (Git)
  • Considered as disposable
  • Not considered movable or copyable


Creating Virtual Environments

Virtual environments are created by executing the venv module:

$ python3 -m venv /path.to/new/virtual/environment

The command has a number of options.


How venvs Work

When a Python interpreter is running from a virtual environment, sys.prefix and sys.exec_prefix point to the directories of the virtual environment, whereas sys.base_prefix and sys.base_exec_prefix point to those of the base Python used to create the environment.

A virtual environment may be activated using a script in its binary directory (bin on POSIX; SCRIPTS on Windows). This will prepend that directory to your PATH, so that running python will invoke the environment's Python interpreter and you can run installed scripts without having to use their full path.

Platform

Shell

Command to activate virtual environment

POSIX

bash/zsh

$ source <venv>/bin/activate

fish

$ source <venv>/bin/activate.fish

csh/tcsh

$ source <venv>/bin/activate.csh

pwsh

$ <venv>/bin/Activate.ps1

Windows

cmd.exe

C:\> <venv>\Scripts\activate.bat

PowerShell

PS C:\> <venv>\Scripts\Activate.ps1

If you don't want to activate the virtual environment, then all scripts that want to use the virtual environment have to have a "shebang" line that points to the virtual environment. This means that the script will run with that interpreter regardless of the value of PATH. When a virtual environment has been activated, the VIRTUAL_ENV environment variable is set to the path of the environment.


Virtual Environments and Packages


It may not be possible for one Python installation to meet the requirements of every application - due to different versions of a library needing to be installed. The solution to this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.

The module used to create and manage virtual environments is called venv. venv will install the Python version from which the command was run. Activating the virtual environment will change your shell's prompt to show which virtual environment you're using, and modify the environment so that running python will get you that particular version and installation of Python.

To deactivate a virtual environment:

$ deactivate


Insert Math Markup

ESC
About Inserting Math Content
Display Style:

Embed News Content

ESC
About Embedding News Content

Embed Youtube Video

ESC
Embedding Youtube Videos

Embed TikTok Video

ESC
Embedding TikTok Videos

Embed X Post

ESC
Embedding X Posts

Embed Instagram Post

ESC
Embedding Instagram Posts

Insert Details Element

ESC

Example Output:

Summary Title
You will be able to insert content here after confirming the title of the <details> element.

Insert Table

ESC
Customization
Align:
Preview:

Insert Horizontal Rule

#000000

Preview:


Insert Chart

ESC

View Content At Different Sizes

ESC

Edit Style of Block Nodes

ESC

Edit the background color, default text color, margin, padding, and border of block nodes. Editable block nodes include paragraphs, headers, and lists.

#ffffff
#000000

Edit Selected Cells

Change the background color, vertical align, and borders of the cells in the current selection.

#ffffff
Vertical Align:
Border
#000000
Border Style:

Edit Table

ESC
Customization:
Align:

Upload Lexical State

ESC

Upload a .lexical file. If the file type matches the type of the current editor, then a preview will be shown below the file input.

Upload 3D Object

ESC

Upload Jupyter Notebook

ESC

Upload a Jupyter notebook and embed the resulting HTML in the text editor.

Insert Custom HTML

ESC

Edit Image Background Color

ESC
#ffffff

Insert Columns Layout

ESC
Column Type:

Select Code Language

ESC
Select Coding Language