29. Dependency Management

Chapter 29 of 36 · 15 min

pip freeze > requirements.txt captures exact versions, but it plasters over every transitive dependency too. Modern tools give you more control.

pip-tools for coordinated dependency management:

# Install pip-tools
pip install pip-tools

# Create requirements.in (your direct dependencies)
# openai>=1.0.0
# pandas>=2.0.0
# matplotlib>=3.7.0

# Compile to requirements.txt with pinned versions
pip-compile -o requirements.txt requirements.in

# Now requirements.txt has openai, pandas, matplotlib AND all their deps
# with pinned versions

# Before creating venv, sync it
pip-sync requirements.txt

poetry as an all-in-one alternative (recommended for new projects):

# Install poetry
curl -sSL https://install.python-poetry.org | python3 -

# Initialize new project
poetry new ai-pipeline
cd ai-pipeline

# Add dependencies
poetry add openai pandas

# Add dev dependencies
poetry add --dev pytest black

# lock and install
poetry install

# Run scripts in the environment
poetry run python script.py
poetry run pytest

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Create a new directory with a pyproject.toml file. Use poetry to initialize it, add requests and click as dependencies, install them, write a small Python script that uses both, and run it with poetry run.