Dependency management is about using tools to handle the libraries and tools your project needs to run. Managing dependencies by hand—such as installing packages one by one or tracking versions in a text file—is error-prone and can easily lead to broken projects, version conflicts, or missing packages.
Tools like poetry
and pip
automate this process, making your code more reliable and easier to share and maintain.
Some pitfalls of managing dependencies by hand:
Imagine you are building a Flask API for your college project. You need Flask, psycopg2 (for PostgreSQL), and maybe some other libraries. If you install everything globally, you might accidentally break another project that needs a different version of Flask or psycopg2. Or, if you share your code with a teammate, it might not work on their laptop because they have different versions installed.
Dependency management tools help you avoid these problems.
pip
(for Python) and poetry
help you install, update, and remove libraries easily.poetry.lock
or requirements.txt
record the exact versions of all libraries your project uses, so everyone on your team has the same setup.Suppose you are working on two projects:
Flask==2.2.0
and psycopg2
.Flask==1.1.2
and a different version of psycopg2
.If you install everything globally, installing one version of Flask will overwrite the other, causing one project to break. With proper dependency management (using virtual environments and lock files), each project gets exactly what it needs, and they don’t interfere with each other.
Poetry is a modern tool for managing Python dependencies and virtual environments. It makes it easy to keep your project isolated and reproducible.
mkdir my-flask-api
cd my-flask-api
mkdir my-flask-api
cd my-flask-api
poetry init
Poetry will ask you some questions. You can press Enter to accept the defaults.
poetry add flask psycopg2
This creates a pyproject.toml
(lists your dependencies) and a poetry.lock
(records exact versions).
poetry install
poetry run python app.py
poetry shell
Note: The
shell
command is no longer included inpoetry
, and is now a separate plugin. See the poetry-plugin-shell GitHub repository for installation instructions.
pyproject.toml
and poetry.lock
files in the GitHub repository. When you clone the repo, run poetry install
to get the exact same environment as everyone else.pyproject.toml
and poetry.lock
files in version control (e.g., git/GitHub).poetry update
poetry remove <package-name>