Title: I can't install the PyGraphviz module in python 3.9.x
Tags: python-3.x,module,networkx,graphviz,pygraphviz

Question: I want to use graph theory in one of my projects, that's why I did some research and I found two modules pretty usefull to do what I want to do.
I found the networkx module that allows me to create some graphs, but I want to create a visualisation of them. So that I will be able to see graphs.
I found another module that make it for me and its name is Graphviz.
The problem is, when I want to run a simple script in Python to create a graph and to visualize it, it says I have to install the PyGraphviz module.
After several attempts, I didn't succeed to install it.
Here is the python script I want to run :
import networkx as nx
from networkx.drawing.nx_agraph import write_dot
import os

# Créer un graphe Escape Game avec NetworkX
G = nx.Graph()
G.add_edge("salle1", "salle2")
G.add_edge("salle2", "salle3")
G.add_edge("salle3", "salle1")

# Exporter le graphe dans un fichier DOT
write_dot(G, "escape_game.dot")

# Utiliser Graphviz pour générer une image du graphe
os.system("dot -Tpng escape_game.dot -o escape_game.png")

And here is the error I get :
Traceback (most recent call last):
File "C:\\Users\\HP\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\networkx\\drawing\\nx_agraph.py", line 133, in to_agraph
import pygraphviz
ModuleNotFoundError: No module named 'pygraphviz'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "c:\\Users\\HP\\Desktop\\Cours_BAC_3\\Projet_individuel\\Juin\\Projet FORM-ESC\\Eg en théorie des graphes\\chatgpt.py", line 12, in \<module\>
write_dot(G, "escape_game.dot")
File "C:\\Users\\HP\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\networkx\\drawing\\nx_agraph.py", line 194, in write_dot
A = to_agraph(G)
File "C:\\Users\\HP\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\networkx\\drawing\\nx_agraph.py", line 135, in to_agraph
raise ImportError(
ImportError: requires pygraphviz http://pygraphviz.github.io/%60

Then, when I want to install the PyGraphviz module with this command :

pip install pygraphviz

Here is the result :
Collecting pygraphviz
Using cached pygraphviz-1.10.zip (120 kB)
Preparing metadata (setup.py) ... done
Building wheels for collected packages: pygraphviz
Building wheel for pygraphviz (setup.py) ... error
error: subprocess-exited-with-error

× python setup.py bdist_wheel did not run successfully.
│ exit code: 1
╰─\> \[48 lines of output\]
running bdist_wheel
running build
running build_py
creating build
creating build\\lib.win-amd64-cpython-39
creating build\\lib.win-amd64-cpython-39\\pygraphviz
copying pygraphviz\\agraph.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz
copying pygraphviz\\graphviz.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz
copying pygraphviz\\scraper.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz
copying pygraphviz\\testing.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz
copying pygraphviz\__init_\_.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz
creating build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_attribute_defaults.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_clear.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_close.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_drawing.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_edge_attributes.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_graph.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_html.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_layout.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_node_attributes.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_readwrite.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_repr_mimebundle.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_scraper.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_string.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_subgraph.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_unicode.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\__init_\_.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
running egg_info
writing pygraphviz.egg-info\\PKG-INFO
writing dependency_links to pygraphviz.egg-info\\dependency_links.txt
writing top-level names to pygraphviz.egg-info\\top_level.txt
reading manifest file 'pygraphviz.egg-info\\SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching '*.png' under directory 'doc'
warning: no files found matching '*.txt' under directory 'doc'
warning: no files found matching '*.css' under directory 'doc'
warning: no previously-included files matching '*\~' found anywhere in distribution
warning: no previously-included files matching '\*.pyc' found anywhere in distribution
warning: no previously-included files matching '.svn' found anywhere in distribution
no previously-included directories found matching 'doc\\build'
adding license file 'LICENSE'
writing manifest file 'pygraphviz.egg-info\\SOURCES.txt'
copying pygraphviz\\graphviz.i -\> build\\lib.win-amd64-cpython-39\\pygraphviz
copying pygraphviz\\graphviz_wrap.c -\> build\\lib.win-amd64-cpython-39\\pygraphviz
running build_ext
building 'pygraphviz.\_graphviz' extension
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
\[end of output\]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for pygraphviz
Running setup.py clean for pygraphviz
Failed to build pygraphviz
Installing collected packages: pygraphviz
Running setup.py install for pygraphviz ... error
error: subprocess-exited-with-error

× Running setup.py install for pygraphviz did not run successfully.
│ exit code: 1
╰─\> \[50 lines of output\]
running install
C:\\Users\\HP\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\setuptools\\command\\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
warnings.warn(
running build
running build_py
creating build
creating build\\lib.win-amd64-cpython-39
creating build\\lib.win-amd64-cpython-39\\pygraphviz
copying pygraphviz\\agraph.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz
copying pygraphviz\\graphviz.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz
copying pygraphviz\\scraper.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz
copying pygraphviz\\testing.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz
copying pygraphviz\__init_\_.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz
creating build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_attribute_defaults.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_clear.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_close.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_drawing.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_edge_attributes.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_graph.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_html.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_layout.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_node_attributes.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_readwrite.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_repr_mimebundle.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_scraper.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_string.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_subgraph.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\\test_unicode.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
copying pygraphviz\\tests\__init_\_.py -\> build\\lib.win-amd64-cpython-39\\pygraphviz\\tests
running egg_info
writing pygraphviz.egg-info\\PKG-INFO
writing dependency_links to pygraphviz.egg-info\\dependency_links.txt
writing top-level names to pygraphviz.egg-info\\top_level.txt
reading manifest file 'pygraphviz.egg-info\\SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching '*.png' under directory 'doc'
warning: no files found matching '*.txt' under directory 'doc'
warning: no files found matching '*.css' under directory 'doc'
warning: no previously-included files matching '*\~' found anywhere in distribution
warning: no previously-included files matching '\*.pyc' found anywhere in distribution
warning: no previously-included files matching '.svn' found anywhere in distribution
no previously-included directories found matching 'doc\\build'
adding license file 'LICENSE'
writing manifest file 'pygraphviz.egg-info\\SOURCES.txt'
copying pygraphviz\\graphviz.i -\> build\\lib.win-amd64-cpython-39\\pygraphviz
copying pygraphviz\\graphviz_wrap.c -\> build\\lib.win-amd64-cpython-39\\pygraphviz
running build_ext
building 'pygraphviz.\_graphviz' extension
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
\[end of output\]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─\> pygraphviz

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

If someone can help me with it, or if you have another python module that does the job, I can take it.
Thanks !

Answer: I'm successfully using import graphviz in my project. But that might be a different package. Note: To actually work it requires the GraphViz application to be installed.