Guide

Create a Python virtual environment

A Python virtual environment keeps a project's installed packages separate from other Python environments.

At a glance

Prerequisite
A Python installation with venv
Creation command
python -m venv .venv

Overview

A Python virtual environment keeps a project's installed packages separate from other Python environments. Create one inside or alongside the project, then use its interpreter when installing dependencies and running the application so the selected packages stay consistent.

1. Create the environment

Open a terminal in the project directory and run python -m venv .venv using the intended Python interpreter. This creates an environment directory containing its own interpreter entry points and package location.

2. Use its interpreter

Activate the environment with the command documented for your shell, or call its interpreter directly. Run package installation through that interpreter, such as python -m pip, to keep the target unambiguous.

3. Check the result

Inspect the interpreter path and installed packages before running the project. Record dependencies in the project's chosen dependency file; recreating an environment should not depend on remembering manual installation steps.

Sources and review

MOOR's explanatory text is supported by the following source links.

  1. venv: virtual environments — Python
  2. Virtual environments and packages — Python

Browse MOOR Knowledge