Skip to main content

Static import graph + PageRank for Python projects

Reason this release was yanked:

Obselete README

Project description

Python Page Rank

Static dependency analysis and PageRank-style importance scoring for Python modules.

This tool scans a Python codebase, builds a directed import graph between modules, and computes a PageRank-like score indicating which modules are most structurally important based on how much other code depends on them.

No code execution and no imports.


Quickstart

python cli.py /path/to/project (or leave blank to run on current path)


What it does

  • Recursively scans a project directory for .py files
  • Parses files using AST (no code execution)
  • Builds a module-level dependency graph from import / from X import Y
  • Computes PageRank over that graph
  • Outputs per-file:
    • lines of code (LOC)
    • number of importers
    • PageRank score

Typical uses:

  • identify central / risky modules
  • guide refactors
  • estimate blast radius of changes
  • understand large or unfamiliar codebases

What it does not do (by design)

  • Execute or import any project code
  • Modify files
  • Resolve runtime-dependent imports
  • Guess ambiguous dependencies

This is a conservative static analyzer.


How imports are handled

Supported

  • import package.module
  • import package.module as alias
  • from package import module
  • from package.subpackage import thing

All resolved to absolute module paths and mapped to files when possible.

Intentionally ignored

from . import models
from .. import utils

These produce no edge in the graph.

Why:
The AST provides no absolute module path (node.module is None). Resolving these requires guessing project roots and can be wrong in multi-root or namespace-package layouts. This behavior is deliberate and covered by tests.


Directory handling

The scanner:

  • automatically detects multiple import roots
  • prefers the shortest valid module path when duplicates exist
  • skips common junk directories:
    • .git, .venv, venv, node_modules, __pycache__, etc.

This allows it to work on:

  • monorepos
  • Django / FastAPI projects
  • repos with multiple top-level packages

PageRank details

  • Standard iterative PageRank
  • Damping factor: 0.85
  • Teleportation distributes rank uniformly
  • Ranks are normalized to sum to 1.0

Interpretation:
A module is "important" if many important modules depend on it.

This measures structural importance, not runtime usage frequency.


Example

python cli.py /path/to/project

Example output:

Module                                     LOC  Importers   PageRank
----------------------------------------------------------------------
MyApp\authentication\models.py              29         12   0.114471
MyApp\common\utils.py                       66          6   0.094118
MyApp\payments\models.py                   168         15   0.053272
...

CLI usage

The tool is intended to be run from the command line.

Syntax

python cli.py [path] [--n N] [--alpha A] [--iters I] [--json]

Arguments

path
Project root directory to scan.
Optional. Defaults to the current directory (.).

--n
Number of top-ranked files to display.
Optional. Default is 10.

--alpha
PageRank damping factor.
Optional. Default is 0.85.

--iters
Number of PageRank iterations to run.
Optional. Default is 50.

--json
Prints results as JSON instead of a text table.


Examples

Analyze the current directory with default settings: python cli.py
Analyze a specific project directory: python cli.py MyApp
Show only the top 10 ranked files: python cli.py --n 10
Use a custom damping factor and iteration count: python cli.py --alpha 0.9 --iters 100
Analyze a directory and output results as JSON: python cli.py MyApp --json
Combine options: python cli.py MyApp --n 20 --alpha 0.8 --iters 75 --json


Notes

  • The analysis is fully static
  • No code is executed
  • Imports are resolved via AST parsing only
  • PYTHONPATH and runtime environment are ignored

Tests

Tests cover:

  • absolute import resolution
  • multi-root handling
  • preference of shortest module paths
  • ignored relative dot-imports
  • stability of PageRank output

Run tests with:

python -m pytest

Limitations (read before filing issues)

  • Relative imports without explicit module paths are ignored
  • Namespace packages may require manual root layout
  • Large repos may take time due to full AST parsing
  • No attempt is made to infer dynamic imports

These are tradeoffs, not bugs.


License

MIT License


Disclaimer / Caveat

If you need aggressive inference, this tool is intentionally not that.

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

python_page_rank-0.1.1.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

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

python_page_rank-0.1.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file python_page_rank-0.1.1.tar.gz.

File metadata

  • Download URL: python_page_rank-0.1.1.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for python_page_rank-0.1.1.tar.gz
Algorithm Hash digest
SHA256 891e52b72572ea3ef88352636f8fc21e4d72a179847eaed9a3f7c56b6ed3f9d3
MD5 fc02062e5b3d5d4353abfa6a4905a30f
BLAKE2b-256 ac791a90a0647a04fbf868f0ccd15fd278bd6956a24fff49e237aa83020537ec

See more details on using hashes here.

File details

Details for the file python_page_rank-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_page_rank-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f869942bc4457679bb63785fc2f8033e0e08411f3fa8633d5c3f778d5ccae208
MD5 5c8da854e6862c421175f10d2aa3144a
BLAKE2b-256 6e13726b081363836151006907e5871f112b3f7f622c0d89cb2d177991ec2c8d

See more details on using hashes here.

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