venv — 虚拟环境的创建

Added in version 3.3.

源代码: Lib/venv/


The venv 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.

When used from within a virtual environment, common installation tools such as pip will install Python packages into a virtual environment without needing to be told to do so explicitly.

A virtual environment is (amongst other things):

  • Used to contain a specific Python interpreter and software libraries and binaries which are needed to support a project (library or application). These are by default isolated from software in other virtual environments and Python interpreters and libraries installed in the operating system.

  • Contained in a directory, conventionally named .venv or venv in the project directory, or under a container directory for lots of virtual environments, such as ~/.virtualenvs .

  • Not checked into source control systems such as Git.

  • Considered as disposable – it should be simple to delete and recreate it from scratch. You don’t place any project code in the environment.

  • Not considered as movable or copyable – you just recreate the same environment in the target location.

PEP 405 for more background on Python virtual environments.

可用性 : not Android, not iOS, not WASI.

This module is not supported on mobile platforms or WebAssembly 平台 .

Creating virtual environments

虚拟环境 are created by executing the venv 模块:

python -m venv /path/to/new/virtual/environment