Last modified: Jun 01, 2025 By Alexander Williams
Install PyGraphviz in Python with Graphviz
PyGraphviz is a Python library for creating and manipulating graph structures. It requires Graphviz as a system dependency. This guide will help you install both.
What is PyGraphviz?
PyGraphviz provides a Python interface to the Graphviz graph visualization software. It is useful for network analysis and visualization.
Before installing PyGraphviz, you need Graphviz installed on your system. This is a critical dependency.
Install Graphviz System Dependency
First, install Graphviz on your operating system. The method varies by platform.
On Ubuntu/Debian
sudo apt-get install graphviz graphviz-dev
On macOS (using Homebrew)
brew install graphviz
On Windows
Download Graphviz from the official website. Add it to your system PATH after installation.
Install PyGraphviz in Python
Once Graphviz is installed, you can install PyGraphviz using pip.
pip install pygraphviz
If you encounter errors, ensure Graphviz is properly installed. For other Python package installations, check our guide on installing Python packages in Docker.
Verify the Installation
To confirm PyGraphviz works, run this Python code.
import pygraphviz as pgv
G = pgv.AGraph()
G.add_node("A")
G.add_edge("A", "B")
print(G.string())
This should output a simple graph description.
Troubleshooting Common Issues
If installation fails, check these common solutions.
Error: "graphviz.h not found" means Graphviz development files are missing. Install them as shown earlier.
For Windows users, ensure the Graphviz bin
folder is in your PATH. Similar path issues may occur when installing packages for Azure Functions.
Basic PyGraphviz Example
Here's a complete example to create and visualize a simple graph.
import pygraphviz as pgv
# Create a new graph
G = pgv.AGraph(directed=True)
# Add nodes and edges
G.add_node("Start")
G.add_node("End")
G.add_edge("Start", "End")
# Save and render
G.draw("example.png", prog="dot")
print("Graph saved as example.png")
This creates a PNG image of your graph. For more complex visualizations, you might need additional tools like WeasyPrint for PDF generation.
Conclusion
Installing PyGraphviz requires Graphviz as a system dependency first. Follow the steps for your OS, then install the Python package.
PyGraphviz is powerful for graph visualization in Python. With it installed, you can create complex network diagrams and analyze relationships in your data.
For other Python installation guides, see our tutorial on installing packages in Google Colab.