By WebNest Studio Engineering Team
Python Tutorial
pip and Dependency Management
Most real Python projects depend on third-party packages from PyPI — requests, pandas, FastAPI, SQLAlchemy. Installing them correctly, keeping versions reproducible across machines, and isolating each project's dependencies are essential professional skills.
This lesson covers pip commands, requirements files, version specifiers, pyproject.toml, and modern tools such as uv and Poetry, building on the virtual environments lesson.
Why it matters
Almost every project depends on packages from PyPI, and "it works on my machine" is usually a dependency problem: a different version is installed somewhere else. Stating exactly which versions a project needs makes builds repeatable, and it is the first thing checked when a deployment fails.
pip Basics
Always run pip as python -m pip so it installs into the interpreter you are using, and always inside an activated virtual environment. Key commands: install, install --upgrade, uninstall, list, show, freeze, and install -r requirements.txt.
Versions and Requirements Files
Version specifiers: ==2.32.3 (exact), >=2.30, ~=2.32 (compatible release: at least 2.32, below 3.0), <3. A requirements.txt lists dependencies one per line. Applications should pin exact versions (a "lock") so every deployment gets identical packages; libraries should declare looser ranges.
pyproject.toml
pyproject.toml is the standard project configuration file: project name, version, Python requirement, dependencies, optional groups (dev, test) and tool settings (Ruff, pytest, mypy). Tools like uv, Poetry, Hatch and pip itself understand it.
uv and Poetry
uv (by Astral) is a very fast, all-in-one tool: it installs Python versions, creates virtual environments, adds dependencies to pyproject.toml, writes a lock file (uv.lock) and runs commands — uv init, uv add fastapi, uv run app.py. Poetry offers a similar workflow. Either is a big improvement over manual pip + requirements files for team projects.
Examples
Everyday pip commands (inside an activated virtual environment)
python -m pip install requests
python -m pip install "fastapi[standard]==0.118.0"
python -m pip install --upgrade requests
python -m pip show requests
python -m pip list --outdated
python -m pip uninstall requests
python -m pip freeze > requirements.txt
python -m pip install -r requirements.txt
Successfully installed requests-2.32.x certifi-... charset-normalizer-... idna-... urllib3-...
Name: requests
Version: 2.32.x
Summary: Python HTTP for Humans.
Requires: certifi, charset-normalizer, idna, urllib3
A requirements file and a pyproject.toml
# requirements.txt (application: pinned versions)
fastapi==0.118.0
uvicorn==0.37.0
sqlalchemy==2.0.43
python-dotenv==1.1.1
# pyproject.toml
[project]
name = "webnest-api"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"fastapi[standard]>=0.115",
"sqlalchemy~=2.0",
]
[dependency-groups]
dev = ["pytest>=8", "ruff>=0.6"]
[tool.ruff]
line-length = 100
(Pin exact versions for deployable apps; declare ranges in libraries.)
A modern workflow with uv
# install uv once: https://docs.astral.sh/uv/ (e.g. pip install uv)
uv init webnest-api
cd webnest-api
uv add "fastapi[standard]" sqlalchemy
uv add --dev pytest ruff
uv run fastapi dev main.py # runs inside the project's managed .venv
uv lock # writes uv.lock with exact versions
uv sync # recreate the exact environment on another machine
Initialized project webnest-api
Resolved 38 packages in 412ms
Installed 38 packages in 96ms
Checking installed package versions from code
from importlib.metadata import version, PackageNotFoundError
for package in ["pip", "surely-not-installed-package"]:
try:
print(package, "->", "installed" if version(package) else "?")
except PackageNotFoundError:
print(package, "-> not installed")
pip -> installed
surely-not-installed-package -> not installed
Common Mistakes
- Installing packages globally instead of in a virtual environment.
- Running pip from a different Python than the project uses; use python -m pip.
- Deploying with unpinned dependencies, so a new release breaks production.
- Committing the .venv folder to Git instead of requirements/lock files.
Exercise
The list holds lines from a requirements file. For each line, print the package name followed by pinned if it fixes an exact version with ==, or not pinned otherwise.
Expected output: requests pinned, flask not pinned, pytest not pinned (one per line)
requirements = ["requests==2.32.0", "flask>=3.0", "pytest"]
for line in requirements:
# TODO: find the package name (the text before any of ==, >=)
# TODO: print the name and "pinned" or "not pinned"
pass
Show hints
- A line is pinned when it contains
==. - Replace
>=with==first, thensplit("==")[0]gives the name in every case.
Show solution
requirements = ["requests==2.32.0", "flask>=3.0", "pytest"]
for line in requirements:
name = line.replace(">=", "==").split("==")[0]
status = "pinned" if "==" in line else "not pinned"
print(name, status)
Quick quiz
1. What does pip install requests==2.32.0 do?
- A. Installs the newest version
- B. Installs any version above 2.32.0
- C. Installs exactly version 2.32.0
- D. Removes the package
Show answer
C. The == operator pins an exact version.
2. Which command installs every package listed in a requirements file?
- A. pip install requirements.txt
- B. pip run requirements.txt
- C. pip freeze requirements.txt
- D. pip install -r requirements.txt
Show answer
D. The -r option tells pip to read the package list from a file.
3. Which file is the modern standard place for a project's metadata and dependencies?
- A. pyproject.toml
- B. setup.cfg
- C. package.json
- D. Pipfile.txt
Show answer
A. pip, uv, Poetry and other tools all read it.
Interview questions
Why should dependency versions be pinned?
Show answer
Without a fixed version, pip installs whatever is newest at the time, so the same project can receive different code on different days or machines, and a new release of a dependency can break it without any change of your own. Pinning exact versions, in a requirements file or a lock file, makes every install identical and every build repeatable.
What is the difference between requirements.txt and pyproject.toml?
Show answer
requirements.txt is a plain list of packages for pip to install, usually with exact versions. pyproject.toml is a structured file that describes the project itself: its name, version, the Python versions it supports, its dependencies as acceptable ranges, and the configuration of its tools. Modern tools combine it with a lock file that records the exact versions installed.
Key Points to Remember
- Use python -m pip inside a virtual environment.
- Version specifiers: ==, >=, ~=, <; pin exact versions for applications.
- requirements.txt lists dependencies; pyproject.toml is the modern project file.
- uv (and Poetry) manage environments, dependencies and lock files in one tool.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
These examples need a project environment on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering