virtual environment
A self-contained folder with its own Python and its own installed packages, so each project's packages stay separate.
In more detail
Create one with python3 -m venv .venv, then activate it so that python and pip in that shell use it and installs land in that folder only. Two projects can then hold different versions of the same package. Make one per project, keep it out of version control, and rebuild it from a requirements file whenever you like.
Examples
The course installs ansible-core into a virtual environment so nothing can damage the system Python.python3 -m venv .venv
source .venv/bin/activateRelated terms
- Python — A popular general-purpose programming language known for readable code and a large standard library.
- pip — Python's standard package installer, which downloads packages from the Python Package Index and installs them.
- source builtin — The builtin that runs a file's commands in the current shell instead of in a new one; the POSIX spelling is a single dot.
- activate script — The script inside a virtual environment that puts it first on PATH for the current shell session.
- requirements file — A text file, usually requirements.txt, listing the packages a project needs, one per line, often pinned to exact versions.
- VIRTUAL_ENV — An environment variable set by the activate script to the folder of the active virtual environment.
- sys.prefix — The folder a running Python treats as its installation; inside a virtual environment it differs from sys.base_prefix.
Taught in
- Installing Ansible — Ansible Configuration Management at Scale
- Installing Python On A Mac — Hands-on Python: Complete Video Course & Book
- Installing Python On Linux — Hands-on Python: Complete Video Course & Book
- Where Python Lives: PATH And Versions — Hands-on Python: Complete Video Course & Book
- Virtual Environments — Hands-on Python: Complete Video Course & Book
- pip, requirements, and installing packages — Hands-on Python: Complete Video Course & Book
- The Upgrade That Broke Nginx: Packages, Repositories And Pins On Both Families — CompTIA Linux+ (XK0-006) Exam Prep
- When awk Runs Out: Python Data Types, Modules And Virtual Environments For Admins — CompTIA Linux+ (XK0-006) Exam Prep
References
- Python docs: venv
- Python Packaging User Guide: virtual environments
- Python tutorial: virtual environments and packages