Skip to main content

PyCG - Practical Python Call Graphs

This is the final release of the Python engine; PyCG continues as a Rust tool. See the repository for more details.

PyCG generates call graphs for Python code with a fully static analysis: it never imports or runs the code that it analyses. The analysis follows the values that a program passes around (functions, classes, instances, containers), so it resolves

  • calls through variables, arguments, return values, attributes and containers (higher-order code)
  • methods through class hierarchies, super(), decorators, properties and descriptors
  • the calls that the interpreter makes without a call expression (for, with, operators, subscripts, await and others)
  • imports inside the package, followed automatically
  • calls into code outside the package (the builtins, the standard library, other packages), named through type stubs

The analysis is fast on packages that use the standard library as external code. It does not yet scale to programs that include large generic libraries such as the standard library itself: see Open problems in docs/design.md, which is the specification of the analysis.

The first version of PyCG is described in the ICSE 2021 paper. The analysis in this repository is a redesign of it; that version is the tag baseline-original-engine. evaluation/ measures PyCG on the micro-benchmark and on a benchmark of real packages: make eval.

You can cite PyCG as follows. Vitalis Salis, Thodoris Sotiropoulos, Panos Louridas, Diomidis Spinellis and Dimitris Mitropoulos. PyCG: Practical Call Graph Generation in Python. In 43rd International Conference on Software Engineering, ICSE '21, 25–28 May 2021.

Installation

PyCG requires Python 3.12 or later and has no runtime dependencies.

This version is not on PyPI yet (pip install pycg gives the 2021 version). Install it from a checkout of this repository:

git clone https://github.com/vitsalis/PyCG
cd PyCG
pip install .

Or run it from the checkout without installing it:

python -m pycg --help            # from the root of the checkout
uv run pycg --help               # with uv, which also makes the environment

Usage

~ >>> pycg -h
usage: pycg [-h] [--package PACKAGE] [--package-name PACKAGE_NAME]
            [--package-version PACKAGE_VERSION] [-o OUTPUT] [--cap N]
            [entry_point ...]

Write the call graph of a Python package as one JSON document.

positional arguments:
  entry_point           Entry points to be processed

options:
  -h, --help            show this help message and exit
  --package PACKAGE     Package root: module names are relative to it and only
                        modules under it are analysed. Default: the deepest
                        directory that contains every entry point
  --package-name PACKAGE_NAME
                        Name of the analysed package
  --package-version PACKAGE_VERSION
                        Version of the analysed package
  -o, --output OUTPUT   Output path
  --cap N               The most objects that the analysis keeps for one
                        variable or expression: past it the value is treated
                        as unknown, and calls through it are reported as
                        unresolved (saturated). 'none' removes the cap: the
                        analysis may then not finish, or run out of memory, on
                        large inputs. Default: 5000

A module that cannot be read or parsed is reported as failed, with a warning, and analysed as an empty module.

--cap (cap= of CallGraphGenerator) is the one setting of the analysis; the document records it. --cap none gives the analysis without a cap, which may not finish on large inputs.

Output

PyCG writes one JSON document, specified in docs/format.md. It lists the analysed modules, their definitions (modules, classes with their MROs, functions), the names that each module exports, the outside names that the code calls, and the call sites of each caller: the position of each call, what it may call, the implicit calls (for, with, operators, properties and others) by construct, and the calls whose callee is not known, with the cause. The document is deterministic: the same input gives the same bytes on every run and every supported Python version.

pycg.formats.edges(document) gives the classic call graph: each node mapped to the sorted names it calls.

{
    "node1": ["node2", "node3"],
    "node2": ["node3"],
    "node3": []
}

Examples

All the entry points are known:

~ >>> pycg --package pkg_root pkg_root/module1.py pkg_root/subpackage/module2.py -o cg.json

All entry points are not known, and the package is named in the document:

~ >>> pycg --package django --package-name django --package-version 5.2 \
        $(find django -type f -name "*.py") -o django.json

Development

Install uv. Then, from the root directory:

uv run make test    # unit tests, the micro-benchmark and its validator
uv run make lint    # ruff check, ruff format --check, no comments
uv run make format  # sort imports and format the code

Each micro-benchmark case is a directory evaluation/micro-benchmark/snippets/<category>/<name>/ that contains a main.py and the expected callgraph.json. The tests find new case directories automatically. To run one case:

uv run pytest "tests/test_micro_benchmark.py::test_call_graph[classes/call]"

The cases that the analysis fails are listed, each with its cause, in tests/known_failures.txt, as strict expected failures. After a change to the analysis, regenerate the list with uv run python tests/triage.py, which holds the causes. It removes the cases that pass now and never adds a case by itself: a failing case that is not in the list is a regression or a new case, and the tool reports it and fails. Give such a case a cause in tests/triage.py and add it with --readmit <category>/<case>.

Metadata

Release files for pycg 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycg 1.0.0
File Size Uploaded Provenance Actions
pycg-1.0.0.tar.gz 2.5 MB With provenance Details for pycg-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pycg 1.0.0
File Interpreter ABI Platform Actions
pycg-1.0.0-py3-none-any.whl Python 3 none any Details for pycg-1.0.0-py3-none-any.whl

Total release size: 5.1 MB

Release files / pycg-1.0.0.tar.gz

Download URL pycg-1.0.0.tar.gz
Size 2.5 MB
Tags Source
SHA-256 checksum
How to use checksums
7f6e1cb1f44728d09096e4cc0d88f76802207a6e4ad025825c76e65bbde30210
BLAKE2b-256 checksum
How to use checksums
aeb69bd9319aa9bf88afcbe09344ac1b621a00a0015efce7e3d0b3794c7eef23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

Release files / pycg-1.0.0-py3-none-any.whl

Download URL pycg-1.0.0-py3-none-any.whl
Size 2.6 MB
Tags Python 3
SHA-256 checksum
How to use checksums
fe3a093cfa22298b50c58bf1ed9a74f45a89b01ed92e6255f60697d4a8ef4886
BLAKE2b-256 checksum
How to use checksums
c9fb11900d9c9208b84cd6feff6e692ee14c57f9dda6efba9dee0551c647fb82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

Security for pycg 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pycg-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page