DEV Community

Cover image for Runtime-based Python dependency tracking with envcore
Jan Bremec
Jan Bremec

Posted on

Runtime-based Python dependency tracking with envcore

I kept running into the same issue with Python dependencies:

  • pip freeze captures the entire environment, often hundreds of packages
  • tools like pipreqs scan source files, but miss runtime imports

Anything conditional, dynamic, or imported indirectly gets lost.

So I built envcore — a small tool that records dependencies by tracing imports while your code actually runs.

How it works

envcore hooks into Python's import system and logs modules as they are loaded during execution. After the run, it resolves those modules to their corresponding PyPI packages and versions.

envcore trace train.py   →   env_manifest.json
envcore restore          →   installs recorded packages
Enter fullscreen mode Exit fullscreen mode

The result is a minimal, runtime-derived manifest.

Example

Instead of exporting a full environment:

numpy       1.26.4
pandas      2.1.4
torch       2.1.0
Enter fullscreen mode Exit fullscreen mode

Only packages that were actually imported during execution are included.

Why runtime tracing

Static analysis misses cases like:

  • imports inside conditional branches
  • imports inside functions
  • dynamic imports (importlib)
  • dependencies imported indirectly by other libraries

envcore observes what Python actually loads, so these cases are captured.

Limitation

envcore traces a single execution path. If some code paths are not executed during tracing, their dependencies will not appear in the manifest. The intended usage is to trace a representative run.

Additional features

  • resolves import → PyPI package names (e.g. PILPillow)
  • works with Jupyter notebooks
  • export to requirements.txt, pyproject.toml, Docker, etc.
  • CLI + programmatic API

Install

pip install envcore
Enter fullscreen mode Exit fullscreen mode

Repository

https://github.com/JanBremec/envcore

Top comments (0)