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

0
0
Asked By MellowKite47 On

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?

5 Answers

Answered By OrbitingPanda52 On

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.

Answered By SilverNectar24 On

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.

Answered By BrightWalnut8 On

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.

Answered By QuietMarble6 On

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.

Answered By CedarFox31 On

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.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.