Python Reference ยท Standard Library: Concurrency, Network and Tools
Virtual environments and pip
How to create and use Python virtual environments with venv, install packages with pip, freeze requirements, and check which interpreter your code runs on.
A virtual environment is a private folder that holds its own Python interpreter link and its own installed packages. Every project gets its own, so two projects can depend on different versions of the same library without fighting each other. The standard tool for making one is the venv module.
pip is the package installer. It downloads packages from the Python Package Index (PyPI) and puts them into whichever environment is active. Together they are the everyday workflow for any Python project beyond a single script.
The commands below are run in a terminal, not inside Python, so they are shown read-only. The live examples further down inspect environments from Python code.
python -m venv .venv
The workflow
Create the environment once per project, activate it in each terminal session, and install what you need.
# 1. Create (inside your project folder) python -m venv .venv # 2. Activate source .venv/bin/activate # macOS and Linux .venv\Scripts\activate # Windows (cmd or PowerShell) # 3. Install and inspect python -m pip install requests python -m pip install "requests>=2.31,<3" python -m pip list python -m pip show requests # 4. Save and restore the exact set of packages python -m pip freeze > requirements.txt python -m pip install -r requirements.txt # 5. Leave the environment deactivate
Use python -m pip rather than a bare pip. It guarantees that pip belongs to the same interpreter you are about to run, which avoids the classic mistake of installing into one Python and running another.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Common pip commands
| Command | What it does |
|---|---|
python -m pip install name |
Installs the latest compatible release. |
python -m pip install "name==1.4.2" |
Installs one exact version. |
python -m pip install --upgrade name |
Upgrades an installed package. |
python -m pip uninstall name |
Removes a package. |
python -m pip list |
Lists installed packages (add --outdated to see updates). |
python -m pip show name |
Shows version, location and what it requires. |
python -m pip freeze |
Prints installed packages as name==version lines. |
python -m pip install -r requirements.txt |
Installs everything listed in a file. |
python -m pip install -e . |
Installs your own project in editable mode, for development. |
python -m venv --help |
Shows the options of venv. |
Usage notes
- Never commit the environment folder. Add
.venv/to.gitignore. Commitrequirements.txt(orpyproject.toml) instead, and recreate the environment anywhere. - Do not move or rename an environment. It stores absolute paths. Delete and recreate it.
- Activation is a convenience. You can also run
.venv/bin/python script.py(or.venv\Scripts\python.exeon Windows) with no activation at all, which is how schedulers and services usually do it. - Checking the active environment:
sys.prefixdiffers fromsys.base_prefixinside a virtual environment, andpython -m pip --versionprints the path of the environment pip belongs to. - Pin versions for applications.
freezeoutput, or a lock file created by tools such aspip-tools,uvor Poetry, makes builds repeatable. Libraries should instead declare loose ranges. - Use one environment per project, not one global one. Installing into the system Python is discouraged; recent Linux distributions refuse it and ask for a virtual environment.
- Options:
--system-site-packageslets the environment see globally installed packages, and--clearempties an existing one. Since Python 3.13 thevenvmodule also writes a.gitignoreinto the new folder. - Modern alternatives such as
uv,pipx(for command-line tools) and Poetry build on the same idea and also create environments. - Pyodide note: the live examples run in a browser sandbox, which has no terminal. There, packages come from
micropipor are preloaded by the runner, andpipitself is not used.
Related
sys,os,importlib,subprocess- Lessons: Virtual environments, Packages and pip