Abstract
Python's "Dependency Hell" often prevents the execution of code snippets found online that lack environment specifications. We present PyCRE (Python Compatible Runtime Environment inference), a framework that automatically reconstructs compatible environments for isolated Python code. PyCRE leverages a large-scale knowledge graph of over 10,000 packages and 300,000 versions, combined with a conflict-aware heuristic solver. Evaluation on the Gistable dataset shows that PyCRE resolves nearly 50% more import errors than existing state-of-the-art tools, typically in under 10 seconds.
Introduction: The Challenge of Dependency Hell
Developers frequently encounter ImportErrors when running "naked" Python snippets from GitHub or StackOverflow. This "Dependency Hell" stems from three primary issues:
- Naming Mismatches: The
importname (e.g.,redcap) often differs from thepip installname (e.g.,pycap). - Missing Metadata: Most snippets lack a
requirements.txtorDockerfile. - Version Conflicts: Conflicting requirements between packages (e.g., one needing
numpy < 1.16and another needingnumpy >= 1.16.4) create complex resolution problems.
Existing tools often rely on static knowledge bases or ignore versioning, leading to environments that fail at runtime.
The PyCRE Framework
PyCRE addresses these challenges through a two-phase process: comprehensive knowledge acquisition and intelligent environment inference.
1. Building the Knowledge Graph (KG)
The core of PyCRE is a massive domain knowledge graph. The researchers performed "knowledge acquisition" by installing and analyzing over 10,000 popular packages and their 300,000+ versions. This KG maps packages to versions, versions to their modules and attributes (functions, classes, variables), and versions to their specific dependencies.
During construction, the authors discovered that approximately 25% of package versions on PyPI fail to install or import correctly. PyCRE indexes these "dead ends" to avoid them during inference.
2. Environment Inference and Conflict Resolution
When given a code snippet, PyCRE follows three main steps:
- Target Code Parsing: Uses Abstract Syntax Trees (AST) to identify every third-party module and attribute called.
- Candidate Discovery: Uses a "matching degree" metric to find libraries in the KG containing the specific attributes used. Attributes serve as "fingerprints"; for example, a specific class might only exist in versions older than a certain release.
- Conflict-Aware Solving: To navigate the NP-complete problem of dependency resolution, PyCRE uses a heuristic graph traversal. It prioritizes the latest stable versions and prunes known conflicting paths, using a formal SAT solver only as a fallback.
Evaluation and Key Results
PyCRE was tested on the Gistable dataset, containing 10,250 single-file Python snippets.
- Superior Error Resolution: PyCRE left only 1,524
ImportErrors, compared to 2,654 for the previous state-of-the-art tool, V2. - Speed: PyCRE averages 7 seconds per inference, significantly faster than V2’s 128-second average, which relies on iterative trial-and-error.
- Conflict Handling: PyCRE successfully resolved version conflicts in all 18 instances encountered in the test set.
Conclusion
PyCRE demonstrates that a deep, attribute-level knowledge graph combined with heuristic solving can effectively automate the reconstruction of Python runtime environments. Future work includes expanding package coverage and addressing non-Python system dependencies.
Summary updated