The `graphviz` library provides a simple pure-Python interface for generating and rendering graph descriptions in the DOT language, which is used by the Graphviz graph-drawing software. It allows users to programmatically create directed and undirected graphs, add nodes and edges, apply attributes for styling, and then render them into various output formats (e.g., PDF, PNG, SVG). The current version is 0.21, and the project maintains an active release cadence with regular updates and Python version support.
pip install graphvizVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create a directed graph, add nodes and edges, and then render it to a PNG file which is automatically opened for viewing. The `Digraph` class is used for directed graphs, and `Graph` for undirected ones. The `render()` method saves the graph to a file and, with `view=True`, opens it using the system's default viewer.
Install the native Graphviz software for your OS (e.g., via `apt`, `dnf`, Homebrew, or official installers) and verify that its `bin` directory is correctly added to your system's PATH. Test by running `dot -V` in your terminal. Restart any IDEs or terminals after updating PATH.
Upgrade your Python environment to Python 3.9 or higher if you intend to use `graphviz` version 0.21 or newer. Check the changelog for specific version compatibility if targeting older `graphviz` releases.
Use raw string literals (e.g., `label=r'hello\nworld'`) for strings containing backslashes. For literal quotes, simply use `label='"'` as of version 0.14+, which handles the escaping automatically. Refer to the 'Backslash escapes' section in the official user guide for detailed examples.
Utilize DOT attributes like `overlap`, `nodesep`, `ranksep`, `rankdir`, `splines`, or explicitly set node ranks to fine-tune the layout. For complex layouts, consider creating invisible nodes/edges to guide the layout engine or using subgraphs.
Install the `graphviz` Python package using pip: `pip install graphviz`.
1. Install the Graphviz system software from the official Graphviz website (graphviz.org/download/). 2. Add the `bin` directory of your Graphviz installation to your system's PATH environment variable. For example, on Windows, this might be `C:\Program Files\Graphviz\bin` or `C:\Program Files (x86)\Graphviz\bin`. On Linux/macOS, ensure it's in a standard PATH location like `/usr/local/bin` or ensure it's installed via a package manager (`sudo apt-get install graphviz` for Debian-based systems, `brew install graphviz` for macOS). 3. Restart your terminal or IDE to ensure the updated PATH is recognized. Alternatively, you can set the `GRAPHVIZ_DOT` environment variable in your script to the exact path of the `dot` executable.
1. Review your DOT graph source code for any syntax errors or inconsistencies. Print `dot.source` to inspect the generated DOT code. 2. Try rendering to a different output format (e.g., SVG, PDF) to see if the issue is specific to PNG or the viewer for that format. For example: `dot.format = 'svg'`. 3. Ensure your Graphviz system installation is complete and healthy by running `dot -V` in your terminal and checking for any warnings or errors. For complex diagrams, ensure all necessary Graphviz tools/layout engines are installed (e.g., `graphviz-dev` on Linux).