Can Python infer dependencies and generate a separate requirements file for each entry point?

August 5, 2026 5 answers
Asked by MellowKite47
I'm working on projects that may be deployed as multiple containers, with each container starting from a different Python entry point. Tools such as pipreqs can scan a directory, but I'm looking for something that can begin with one script, follow its local imports, and infer the external packages needed for that entry point alone. Ideally, it would generate separate dependency lists without including unrelated packages from the rest of the repository. Does a reliable tool already exist, or would this be a reasonable project to build?

Answers

CedarFox31
You can also put dependency metadata directly in a standalone script and let uv create the environment when running it. That works well when each entry point is effectively its own script, and it avoids maintaining a separate requirements file.
Reply to this answer
QuietMarble6
If you specifically want inference, start with Pythonโ€™s ast module. Parse the entry point, collect its imports, then recursively inspect local modules. The difficult part is mapping import names to package names, handling optional or dynamic imports, and deciding whether dependencies of third-party packages should be included. If a required package is not installed locally, static analysis cannot inspect it reliably.
Reply to this answer
BrightWalnut8
Pigar is probably the closest match for this specific task. It analyzes imports and generates a requirements file, although it isnโ€™t perfect and may need some manual cleanup.
Reply to this answer
SilverNectar24
For smaller repositories, manually maintaining separate files is often the least surprising solution. Tools can miss conditional imports, plugins, runtime loading, and package-name differences, while pip freeze usually includes unrelated packages. Another practical approach is to run each entry point in a clean environment and add dependencies as missing-import errors reveal them.

VividCactus90: That manual workflow is reasonable, but the appeal of an analyzer is reducing the initial setup work. It could generate a first draft that a developer reviews rather than pretending the result is guaranteed to be complete.

Reply to this answer
OrbitingPanda52
For a modern project, Iโ€™d avoid generating requirements files from the environment. Use a pyproject.toml with separate dependency groups, such as api and worker, then use a lock or export tool to create the environment for each container. This is more explicit and generally more reproducible than pip freeze.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions