alex
alex

Reputation: 3128

Visualize the structure of a Python module

Is there tool out there which can be used to graphically represent the structure of a python module?

I'm thinking that a graph of sub-modules and classes connected by arrows representing imports.

Upvotes: 22

Views: 10241

Answers (4)

Prakhar Sharma
Prakhar Sharma

Reputation: 758

I know this question is very old. If someone is searching a tool for visualising the structure of a Python module, you can try Sourcetrail.

This tool helps you to visualise big source codes such as PyTorch or TensorFlow.

It starts with a nice overview of the project.

enter image description here

If you go inside the Classes button and select any class, it will show you the connection of a specific class with other classes and functions.

enter image description here

All the buttons are clickable and gives you a nice overview. This gives you a kickstart to understand the structure of unknown source code very easily.

Upvotes: 2

Ilia Gilmijarow
Ilia Gilmijarow

Reputation: 1020

An IDE for Python, called ERIC used to have this very functionality

Upvotes: 0

user626998
user626998

Reputation:

I think you want the Python library snakefood

sfood: Given a set of input files or root directories, generate a list of dependencies between the files;

sfood-graph: Read a list of dependencies and produce a Graphviz dot file. (This file can be run through the Graphviz dot tool to produce a viewable/printable PDF file);

Upvotes: 15

JAB
JAB

Reputation: 21089

Not sure if there's a tool, but you could always parse the package you're working with, walking the directory subtree and recording the classes for each module, and also recording the imports. (If you're working with a single module, then it'd just be checking the classes and inputs, no path-walking required.) Then you'd have to iteratively or recursively (Python recommends iteration) check each module imported for anything imported by them until there are no more imports to make (be careful about circular imports!).

You'll probably find pydot or something similar quite useful for graphically displaying the structure.

Upvotes: 0

Related Questions