pip & Virtual Environments
The Python Package Index (PyPI) hosts over half a million third-party packages. pip installs them, and virtual environments keep each project's packages separate so projects never clash. This lesson covers the standard workflow and the newer tools many teams now use.
Why virtual environments?
Without them, every package goes into one shared installation. Project A needs Django 4, project B needs Django 5, and they can't both be installed. A virtual environment ("venv") is a self-contained folder with its own Python and its own packages, one per project.
Creating and activating a venv
cd my-project python3 -m venv .venv
Then activate it:
| System | Command |
|---|---|
| macOS / Linux | source .venv/bin/activate |
| Windows (PowerShell) | .venv\Scripts\Activate.ps1 |
| Windows (cmd) | .venv\Scripts\activate.bat |
Your prompt changes to show (.venv), and python and pip now refer to the environment's copies. Type deactivate to leave. Editors like VS Code detect .venv folders automatically. Add .venv/ to .gitignore — environments are recreated, not committed.
Installing packages with pip
pip install requests
pip install "django>=5.1,<6"
pip install --upgrade requests
pip uninstall requests
pip list
pip show requests
Version specifiers like >=5.1,<6 accept compatible versions while avoiding major upgrades that might break your code.
Once installed, a package is imported like any module:
import requests response = requests.get("https://api.github.com/repos/python/cpython", timeout=10) response.raise_for_status() data = response.json() print(data["full_name"], "has", data["stargazers_count"], "stars")
Recording dependencies
Other people (and your future self, and your deployment server) need to install the same packages. The classic approach is a requirements.txt file:
pip freeze > requirements.txt pip install -r requirements.txt
certifi==2026.8.12 charset-normalizer==3.4.3 idna==3.10 requests==2.32.5 urllib3==2.5.0
pip freeze pins exact versions of everything installed, including indirect dependencies, so installs are reproducible.
pyproject.toml
Modern projects describe themselves in a pyproject.toml file, which lists the direct dependencies and metadata, and is understood by every modern Python tool:
[project] name = "weather-report" version = "0.1.0" requires-python = ">=3.12" dependencies = [ "requests>=2.32", "rich>=13", ] [project.optional-dependencies] dev = ["pytest>=8", "ruff"]
pip install -e ".[dev]"
-e installs your own project in "editable" mode, so code changes take effect without reinstalling.
uv: a faster all-in-one tool
uv has quickly become popular because it replaces pip, venv and version managers with one very fast tool:
uv init weather-report # create a project with pyproject.toml cd weather-report uv add requests rich # add dependencies (creates .venv automatically) uv add --dev pytest uv run main.py # run inside the project's environment uv python install 3.14 # install a specific Python version
uv also writes a uv.lock file pinning every exact version, so everyone on a team gets an identical environment. Poetry and PDM are other tools built around the same pyproject.toml idea.
Running tools without installing them globally
pipx install ruff # install a command-line tool in its own isolated environment uvx ruff check . # or run it once without installing
Choosing packages wisely
- Check that a package is actively maintained and widely used (recent releases, documentation, download counts).
- Type package names carefully: attackers publish "typosquatting" packages with names one letter off from popular ones.
- Prefer the standard library when it does the job — every dependency is something to keep updated.
- Run
pip list --outdated(oruv lock --upgrade) regularly and update deliberately.
Packages in the browser editor
The in-page editors on this site run Pyodide, which bundles the standard library and can load many popular pure-Python and scientific packages. On your own computer, use a virtual environment for every project — it's a habit that saves hours of confusing version problems.