How to import an existing requirements.txt into a Poetry project?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Migrating an existing requirements.txt file into Poetry is a common modernization step for Python projects. The goal is to preserve dependency intent while moving to Poetry-managed lock files and reproducible environments. A careful import workflow avoids version drift and duplicate dependency declarations.
Start with a Clean Poetry Project
If Poetry config does not exist yet:
If project already has pyproject.toml, confirm current dependencies first:
This helps you avoid importing duplicates blindly.
Import Requirements with poetry add
A practical approach is filtering comments and passing package specifiers to Poetry.
This works for straightforward requirement lines such as pinned versions and range specifiers.
Handle Dev Dependencies Separately
If you have requirements-dev.txt, import into Poetry dev group.
Grouping keeps runtime and development dependencies clearly separated.
Validate and Lock
After import, create or refresh lock file and validate installs.
Then run your test suite to ensure dependency resolution still matches project behavior.
Handle Complex Requirement Lines
Some requirement files include:
- editable installs
- local paths
- VCS references
- environment markers
Poetry supports many of these, but syntax may differ from pip requirements format. For difficult lines, add dependencies manually in pyproject.toml and run poetry lock again.
For example, VCS dependency:
Migration Workflow for Teams
For team migrations, use a branch and commit each step:
- import runtime requirements
- import dev requirements
- run tests and static checks
- remove legacy requirements files if policy allows
- document Poetry commands in README
This incremental approach simplifies review and rollback.
Automating Import with a Scripted Workflow
For repeatability, teams often automate import and validation in a one-time migration script.
After script execution, run tests and compare dependency trees between old and new environments. If package resolution differs, tighten version constraints in pyproject.toml and lock again.
For multi-service repositories, migrate one service at a time. This keeps review scope manageable and avoids organization-wide dependency shifts in a single pull request.
Post-Migration Cleanup
After successful migration, decide whether to keep requirements.txt as an export artifact or remove it entirely. If your organization still requires it for certain deploy tools, generate it from Poetry rather than editing two dependency sources manually.
One-way export keeps Poetry as source of truth and avoids dependency drift between files.
Common Pitfalls
A common pitfall is importing everything into default group, including test and lint dependencies.
Another issue is trusting that identical version specifiers always resolve to identical transitive trees. Lock file differences can still occur.
Developers also forget to update CI scripts from pip install -r to poetry install.
Finally, avoid keeping stale duplicate dependency sources long-term unless intentionally documented.
Summary
- Initialize or inspect Poetry project before importing.
- Import clean requirement lines with
poetry add. - Separate dev dependencies into explicit groups.
- Lock and test after migration to verify behavior.
- Update CI and documentation to standardize on Poetry workflows.

