Skip to main content

Automatic Python environment reconstruction — runtime import tracing, snapshot, and restore.

Project description

Envcore

Runtime import tracing for Python. Know exactly what your code needs.
Trace · Snapshot · Restore · Audit

PyPI License Python CI


Why Envcore

pip freeze dumps your entire environment — 200+ packages, most of which your code never touches. Static analysers like pipreqs scan source files but miss dynamic imports, conditional imports, and anything loaded at runtime.

Envcore takes a different approach: it hooks into Python's import system while your code runs, records only the packages that are actually imported, resolves them to their PyPI names and pinned versions, and writes a deterministic manifest. One command rebuilds that exact environment anywhere.

Trace  →  env_manifest.json  →  Restore

No configuration files to maintain. No guesswork.


Install

pip install envcore

Development setup:

git clone https://github.com/JanBremec/envcore.git
cd envcore
pip install -e ".[dev]"

Requires Python 3.9+.


Quick Start

Trace your script to capture its runtime dependencies:

envcore trace train.py
  envcore trace  train.py

    Traced 3 packages:

      numpy 1.26.4
      pandas 2.1.4
      torch 2.1.0

    Manifest saved to env_manifest.json

Restore the environment on any machine:

envcore restore

That is all you need. The manifest ships with your code, and envcore restore installs exactly what was traced — nothing more.


Commands

Command What it does
envcore trace <script> Run a script with import tracing, save manifest
envcore watch <script> Live import tracing with real-time manifest updates
envcore notebook <.ipynb> Trace imports from a Jupyter notebook
envcore snapshot Save imports from programmatic tracking to manifest
envcore restore Install all packages from manifest
envcore show Pretty-print manifest contents
envcore diff <a> <b> Compare two manifests
envcore export Export to requirements.txt, conda, Docker, and more
envcore sync Diff manifest against requirements.txt or pyproject.toml
envcore doctor Environment health check: missing, outdated, orphans
envcore clean Remove packages not tracked in manifest
envcore minimize Find the minimal top-level install set
envcore lock Generate lockfile with SHA-256 hashes
envcore graph Dependency graph (ASCII tree, Mermaid, Graphviz DOT)
envcore audit Scan for known vulnerabilities via OSV.dev
envcore ci Verify environment matches manifest (exit 0 or 1)
envcore hooks install Install git pre-commit hook
envcore history Manifest version history and snapshots
envcore init Create a blank manifest

Every command supports --help. Set NO_COLOR=1 to disable coloured output.


Export Formats

envcore export -f requirements   # requirements.txt
envcore export -f pyproject      # pyproject.toml [project.dependencies]
envcore export -f conda          # environment.yml
envcore export -f docker         # Dockerfile with pinned installs
envcore export -f pipfile        # Pipfile
envcore export -f setup          # setup.py

Health Check

envcore doctor
  Python    3.11.5
  Packages  18

    torch       required 2.1.0, not installed       [missing]
    numpy       manifest 1.25.0 != installed 1.26.4 [mismatch]
    flask       3.0.0 -> 3.1.0 available            [outdated]
    requests    installed 2.31.0, not in manifest    [orphan]

  1 critical, 2 warnings

Detects missing packages, version mismatches, orphaned installs, outdated dependencies, and stale manifests.


Minimize

Reduce a traced environment to its minimal top-level install set:

envcore minimize
  18 packages traced -> 3 top-level installs

  Top-level
    numpy 1.26.4
    pandas 2.1.3
    scikit-learn 1.3.2

  Transitive (auto-installed)
    scipy 1.11.3
    joblib 1.2.0
    threadpoolctl 3.2.0

  Minimal install command:
  pip install numpy==1.26.4 pandas==2.1.3 scikit-learn==1.3.2

Security

envcore lock    # Lockfile with SHA-256 integrity hashes
envcore audit   # Scan packages against OSV.dev vulnerability database

Dependency Graph

envcore graph              # ASCII tree
envcore graph -f mermaid   # Mermaid (renders in GitHub READMEs)
envcore graph -f dot       # Graphviz DOT
  pandas 2.1.3
  +-- numpy 1.26.4
  +-- python-dateutil 2.8.2
  scikit-learn 1.3.2
  +-- joblib 1.2.0
  +-- numpy 1.26.4
  +-- scipy 1.11.3
  +-- threadpoolctl 3.2.0

CI/CD and Git Hooks

envcore ci              # Exit 0 if environment matches, 1 otherwise
envcore hooks install   # Pre-commit hook: auto-check manifest on commit
envcore sync            # Diff manifest vs requirements.txt
envcore sync --apply    # Overwrite requirements.txt from manifest

Jupyter Notebooks

envcore notebook analysis.ipynb            # Trace imports at runtime
envcore notebook analysis.ipynb --static   # Static analysis only

Inside IPython or Jupyter:

%load_ext envcore
%envcore start
import numpy, pandas
%envcore snapshot

Programmatic API

import envcore

envcore.start_tracking()

import numpy
import pandas
from sklearn.model_selection import train_test_split

envcore.stop_tracking()
envcore.snapshot("env_manifest.json")

Context manager:

from envcore import ImportTracer

tracer = ImportTracer()
with tracer:
    import numpy
    import pandas

print(tracer.module_names)  # ['numpy', 'pandas']

Decorator:

from envcore import ImportTracer

tracer = ImportTracer()

@tracer
def train():
    import torch
    import wandb

train()
print(tracer.module_names)  # ['torch', 'wandb']

How It Works

1. TRACE
   builtins.__import__  -->  ImportTracer hook
   Records: module name, timestamp, order
   Filters: stdlib, pre-existing modules

2. RESOLVE
   import name  -->  PyPI package + version
   "PIL"        -->  Pillow 10.2.0
   "cv2"        -->  opencv-python 4.9.0
   "sklearn"    -->  scikit-learn 1.4.0

3. SNAPSHOT
   env_manifest.json
   { "numpy": "1.26.4", "torch": "2.1.0", ... }

4. RESTORE
   pip install numpy==1.26.4 torch==2.1.0 ...

By tracing at runtime, Envcore captures dynamic imports, conditional imports, and imports inside functions — things static analysis tools miss entirely.


Configuration

Project-level settings in pyproject.toml:

[tool.envcore]
manifest = "env_manifest.json"
exclude = ["setuptools", "pip", "wheel"]
entry_points = ["src/main.py", "src/api.py"]
auto_export = "requirements.txt"

Comparison

Feature Envcore pipreqs pip freeze
Detection method Runtime tracing Static analysis Environment dump
Dynamic imports Yes No N/A
Conditional imports Yes No N/A
Filters stdlib Yes Yes No
Filters unused packages Yes Yes No
Import alias handling Yes Partial Yes
One-command restore Yes No No
Export (6 formats) Yes No No
Health check Yes No No
Dependency minimization Yes No No
Lockfile with hashes Yes No No
Dependency graph Yes No No
Vulnerability scanning Yes No No
Jupyter support Yes No No
CI/CD integration Yes No No
Git hooks Yes No No

Contributing

Contributions welcome. Please open an issue or pull request.

git clone https://github.com/JanBremec/envcore.git
cd envcore
pip install -e ".[dev]"
pytest tests/ -v

License

MIT — Jan Bremec

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envcore-0.1.0.tar.gz (48.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

envcore-0.1.0-py3-none-any.whl (43.9 kB view details)

Uploaded Python 3

File details

Details for the file envcore-0.1.0.tar.gz.

File metadata

  • Download URL: envcore-0.1.0.tar.gz
  • Upload date:
  • Size: 48.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for envcore-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f0846f09cccb4318c876f3a8f167fcbe88c9328055f4dc7b6645d47664ad5dbf
MD5 75c8c5ce05ae47215cda365d37781652
BLAKE2b-256 5a98f2aaf1edbb101646453e7bc6ec28f2ad571edaaa525bcd0d74971575e179

See more details on using hashes here.

Provenance

The following attestation bundles were made for envcore-0.1.0.tar.gz:

Publisher: ci.yml on JanBremec/envcore

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file envcore-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: envcore-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 43.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for envcore-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d856a98ac005e623f399c615eea1d7482e4066a2622b46a15d457677fc276173
MD5 dac1f28432637671b29e64049f4dd745
BLAKE2b-256 d856c654361542ecdf1eb04e124852b78265fbc69024d1fe8257e3db2bf28bce

See more details on using hashes here.

Provenance

The following attestation bundles were made for envcore-0.1.0-py3-none-any.whl:

Publisher: ci.yml on JanBremec/envcore

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page