What's the best way to generate a UML diagram from Python source code? [closed]
Asked Answered
M

9

395

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?

Menarche answered 3/11, 2008 at 22:8 Comment(9)
Mods did not find this question constructive. I(and many others who have favourited this) found it useful. So what? that is not a bigger deal than this not being "constructive"!Blazon
Not constructive because it will solicit debates, arguments, discussions ?????? Isn't that what we want ? This is a very relevant question..Innumerable
@yatisagade if you note it was closed, not deleted (and with this many upvotes probably can practically never be deleted). Closing in just means it can't get new answers.Annieannihilate
@Innumerable No, we don't want discussion, we want question and answer pairs that have clear answers. It is not a judgement on it being an interesting question or a useful question, it is a matter of being on-topic for SO. Surely you agree that "What is the best editor?" (obs emacs) is not a constructive question, this question is of exactly the same mold.Annieannihilate
@Annieannihilate Some discussion is very relevant when it comes to refining questions into a Q&A format.Bramante
@Annieannihilate I came here looking for "a" way to generate UML diagrams, not "the best" way, and I'm sure so have others. So instead of just closing it, mods could (should?) suggest alternative ways that fit better. That would be more constructive, wouldn't it?Yvonneyvonner
Instead of trying to get people to stop asking certain obviously normal questions, they should restructure their rules to allow these kindsa questions. Maybe just simply put them into an opinion section instead of just slapping a big NO to it?Coronagraph
In case people are still looking for an easy solution as of 2021. Pycharm has Class Diagrams integrated: jetbrains.com/help/pycharm/viewing-diagram.htmlGush
Yes, thank you! This question is still relevant in 2021. I checked out Pycharm and the class diagram capability looks great! However, just FYI, it is only available in the Pro version, not the free community version.Unscathed
G
246

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.

Gyrate answered 26/9, 2011 at 11:36 Comment(8)
do you know how to visualize private methods starting with "_"Bandurria
Unfortunately, pyreverse package diagrams are huge since everything is placed horizontally (more of a graphviz limitation, but still). Not useful for including in documents.Arbuckle
@Arbuckle as a workaround, you can cherry pick the modules you want to include, and create several separate graphs.Burney
should the obvious choice for the output type not be -o pdf? scalable, searchable.Throat
There are a lot of old links that point to manually downloading and installing an old version of pylint. The up-to-date version can be found through pip install pylint or conda install pylint if you have conda-forge as one of your channels.Bialy
@Bandurria Just pass the option --filter-mode=ALLArronarrondissement
Pyreverse seems dead since 2005.Gynecoid
link is broken , the docs are at pylint.pycqa.org/en/latest/pyreverse.htmlDuel
G
105

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.

Gusgusba answered 3/11, 2008 at 23:26 Comment(7)
Is there any way to have it output vector graphics instead of gifs? I found no documentation on that and the graphs are fairly useless for anything other than html doc.Arbuckle
Presently, epydoc seems unable to generate graphs. Check out this bug report.Sabol
doesn't work with python 3Hiro
I just tested with python 3.7.1 and could generate some diagramsVenenose
@Venenose how did you use it? my python complained print '\n' + msg.rstrip() + '\n' SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?K
@PabloLION Update print statement as : print('\n' + msg.rstrip() + '\n')Christmastide
Yea I know. This is the python3 syntax. print in Python2 doesn't need (). And this is why I found it weird that @Venenose could use it.K
I
14

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.

Integrity answered 3/11, 2008 at 22:19 Comment(3)
Some good things, but the halt-solving hand-waiving ruins it. Pathological cases are not the matter here. Well-behaved is enough.Observable
What do you mean, "hand-waiving"? I didn't write out the full proofs, but I gave enough information that anyone who has seen similar proofs could create them, one for composition and one for inheritance.Integrity
Here's an analogy: diff/patch can fail in a lot of different ways, some of them trivial. It is still very useful in a lot of real world cases. In reasonable cases, diagramming inheritance is trivial. Delegation is more tricky, but feasible by type inference within the boundaries of a package.Observable
O
8

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

Orangutan answered 3/11, 2008 at 22:15 Comment(4)
That's a really good suggestion, but FWIW I notice on the PyUML project site that they don't support Eclipse 3.4 (Ganymede) yet. I'll look forward to trying it when they work that out.Hallucination
Did you happen to get PyUML work with 3.4?Gomar
The last commits to this project date back to 2009. It does not show up in the Marketplace and Eclipse is not to able to install it from the .zip archive.Sabol
It's a shame, it's a great name :)Marx
D
8

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

Disjoin answered 4/11, 2008 at 13:12 Comment(0)
I
6

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

vipera

Best regards.

Ivonne answered 9/12, 2011 at 12:21 Comment(0)
D
5

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.

Dyscrasia answered 4/11, 2008 at 2:37 Comment(1)
sadly it has stopped developping, but still works here ! (2013)Isahella
T
5

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

Turtleneck answered 4/11, 2008 at 21:53 Comment(0)
B
5

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...

Buford answered 7/7, 2011 at 6:43 Comment(2)
do you know of a way to make Umbrello generate the complete class diagramm (and the relations between them, not just the classes by themselves - don't remember how that diagram is called)Unkind
AAhh, you need to import the files and then when you drag and drop classes into the drawing area, the connections are automatically added.Buford

© 2022 - 2024 — McMap. All rights reserved.