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
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.
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.
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.
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.
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.

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.