liveBook · Manning

Virtual environment

As explained in The Quick Python Book, Fourth Edition

A virtual environment is an isolated Python installation with its own interpreter and packages, stored in a self-contained directory structure.

A virtual environment is a self-contained directory containing a Python interpreter, pip, and additional packages. Its isolated libraries prevent conflicts with system Python or other projects, so applications can use different Python and package versions. section 19.5.2

Create and use one in two steps:

  • Create it with `python -m venv test-env`. section 19.5.2
  • Activate it with `test-env\Scripts\activate.bat` on Windows or `source test-env/bin/activate` on Unix and macOS. section 19.5.2
  • After activation, install packages with pip, such as `pip install requests`; the environment's Python version remains the one used during creation. section 19.5.2

Project-specific requests installation

Run `python -m venv test-env`, activate `test-env`, then run `pip install requests`. The resulting package belongs to that environment rather than the system installation. section 19.5.2

myuser@mymachine$ python -m venv test-env

Use the command listing to create a `test-env` directory with Python and pip installed. section 19.5.2

(test-env) myuser@mymachine$ pip install requests   #1

Use the activated prompt to install `requests` inside the virtual environment. section 19.5.2

Virtual environments are standard practice for managing dependencies across multiple projects. Colaboratory sessions are temporary environments, so they generally do not require separate virtual-environment management. section 19.5 section 19.5.2

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Defines the directory structure and gives creation, activation, and package-installation commands, without descending into implementation internals.
Examples
Uses `test-env`, platform-specific activation commands, and installing the requests package.
What it assumes
Assumes a self-managed Python installation and command-line familiarity; distinguishes Colaboratory's temporary sessions from persistent environments.
What you can do after
Operate/configure isolated Python environments for projects with different interpreter versions and package dependencies.
How it connects
Connects Python interpreters, pip, package dependencies, Python Package Index, and the venv module.

Also explained in

Compare how 7 books explain virtual environment →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.