387

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mike Pirnat
  • 3,871
  • 3
  • 17
  • 7
  • 72
    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"! – yati sagade Jul 31 '13 at 10:57
  • 27
    Not constructive because it will solicit debates, arguments, discussions ?????? Isn't that what we want ? This is a very relevant question.. – Bhushan Jan 30 '14 at 00:52
  • 3
    @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. – tacaswell Apr 09 '14 at 15:50
  • 10
    @Bhushan 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. – tacaswell Apr 09 '14 at 15:54
  • 1
    @tacaswell Some discussion is very relevant when it comes to refining questions into a Q&A format. – user32882 Sep 14 '19 at 12:32
  • 17
    @tacaswell 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? – antonimmo Jul 03 '20 at 09:17
  • 7
    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? – wamster Nov 18 '20 at 00:59
  • 1
    In case people are still looking for an easy solution as of 2021. Pycharm has Class Diagrams integrated: https://www.jetbrains.com/help/pycharm/viewing-diagram.html – Guido Jul 29 '21 at 10:01
  • 1
    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. – Gerhard Sep 22 '21 at 01:27

9 Answers9

228

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.

ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58
Nicolas Chauvat
  • 2,313
  • 1
  • 13
  • 2
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.

piro
  • 13,378
  • 5
  • 34
  • 38
13

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.

Andru Luvisi
  • 24,367
  • 6
  • 53
  • 66
  • 12
    Some good things, but the halt-solving hand-waiving ruins it. Pathological cases are not the matter here. Well-behaved is enough. – ddaa Nov 03 '08 at 22:24
  • 1
    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. – Andru Luvisi Nov 03 '08 at 22:31
  • 22
    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. – ddaa Nov 03 '08 at 22:59
8

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

bad_coder
  • 11,289
  • 20
  • 44
  • 72
David Arcos
  • 5,957
  • 5
  • 30
  • 39
  • 1
    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. – Bill Karwin Nov 03 '08 at 22:35
  • Did you happen to get PyUML work with 3.4? – anijhaw Feb 04 '11 at 03:27
  • 3
    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. – Luís de Sousa Jul 20 '16 at 14:39
8

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

Ahmed Rezk
  • 301
  • 1
  • 4
  • 10
Ali Afshar
  • 40,967
  • 12
  • 95
  • 109
6

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

vipera

Best regards.

Ángel Luis
  • 61
  • 1
  • 1
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.

crystalattice
  • 5,031
  • 12
  • 40
  • 57
5

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

chimp
  • 1,823
  • 9
  • 14
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...

Hosane
  • 915
  • 9
  • 19
  • 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) – vlad-ardelean Jun 24 '13 at 20:17
  • 2
    AAhh, you need to import the files and then when you drag and drop classes into the drawing area, the connections are automatically added. – Hosane Jun 28 '13 at 21:44