What's the best way to generate a UML diagram from Python source code?

PythonUmlDiagram

Python Problem Overview


A colleague is looking to generate UML class diagrams from heaps of Python source code. He's primarily interested in the inheritance relationships, and mildly interested in compositional relationships, and doesn't care much about class attributes that are just Python primitives.

The source code is pretty straightforward and not tremendously evil--it doesn't do any fancy metaclass magic, for example. (It's mostly from the days of Python 1.5.2, with some sprinklings of "modern" 2.3ish stuff.)

What's the best existing solution to recommend?

Python Solutions


Solution 1 - Python

You may have heard of Pylint that helps statically checking Python code. Few people know that it comes with a tool named Pyreverse that draws UML diagrams from the Python code it reads. Pyreverse uses Graphviz as a backend.

It is used like this:

pyreverse -o png -p yourpackage .

where the . can also be a single file.

Solution 2 - Python

Epydoc is a tool to generate API documentation from Python source code. It also generates UML class diagrams, using Graphviz in fancy ways. Here is an example of diagram generated from the source code of Epydoc itself.

Because Epydoc performs both object introspection and source parsing it can gather more informations respect to static code analysers such as Doxygen: it can inspect a fair amount of dynamically generated classes and functions, but can also use comments or unassigned strings as a documentation source, e.g. for variables and class public attributes.

Solution 3 - Python

Check this list of seven uml tools for python

Solution 4 - Python

Certain classes of well-behaved programs may be diagrammable, but in the general case, it can't be done. Python objects can be extended at run time, and objects of any type can be assigned to any instance variable. Figuring out what classes an object can contain pointers to (composition) would require a full understanding of the runtime behavior of the program.

Python's metaclass capabilities mean that reasoning about the inheritance structure would also require a full understanding of the runtime behavior of the program.

To prove that these are impossible, you argue that if such a UML diagrammer existed, then you could take an arbitrary program, convert "halt" statements into statements that would impact the UML diagram, and use the UML diagrammer to solve the halting problem, which as we know is impossible.

Solution 5 - Python

It is worth mentioning Gaphor. A Python modelling/UML tool.

Solution 6 - Python

If you use Eclipse, maybe PyUML. Haven't used it, though.

Solution 7 - Python

vipera is a small application designer, and uml is included. You can see it in:

vipera

Best regards.

Solution 8 - Python

The SPE IDE has built-in UML creator. Just open the files in SPE and click on the UML tab.

I don't know how comprhensive it is for your needs, but it doesn't require any additional downloads or configurations to use.

Solution 9 - Python

Sparx's Enterprise Architect performs round-tripping of Python source. They have a free time-limited trial edition.

Solution 10 - Python

Umbrello does that too. in the menu go to Code -> import project and then point to the root deirectory of your project. then it reverses the code for ya...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMike PirnatView Question on Stackoverflow
Solution 1 - PythonNicolas ChauvatView Answer on Stackoverflow
Solution 2 - PythonpiroView Answer on Stackoverflow
Solution 3 - PythonJordi CabotView Answer on Stackoverflow
Solution 4 - PythonAndru LuvisiView Answer on Stackoverflow
Solution 5 - PythonAli AfsharView Answer on Stackoverflow
Solution 6 - PythonDavid ArcosView Answer on Stackoverflow
Solution 7 - PythonÁngel LuisView Answer on Stackoverflow
Solution 8 - PythoncrystalatticeView Answer on Stackoverflow
Solution 9 - PythonchimpView Answer on Stackoverflow
Solution 10 - PythonHosaneView Answer on Stackoverflow