Trouble with pydot in C2_W4_Lab_01_Decision_Trees

I’ve tried to run the first lab in Week 4 on my own device, with all the files from the course downloaded, and I ran into a problem with pydot.
At first I didn’t have the module, so I installed the following which I needed: pydot-1.4.2, pandas-2.0.3, networkx-3.1.

Now I get the following error: FileNotFoundError: [WinError 2] “dot” not found in path.
And I have:
FileNotFoundError Traceback (most recent call last)
File ~\AppData\Roaming\Python\Python310\site-packages\pydot.py:1923, in Dot.create(self, prog, format, encoding)
Where it highlights call_graphviz
And in:
pydot.py:132, in call_graphviz(
Where it highlights nothing
Then in C:\ProgramData\miniconda3\lib\subprocess.py:971, in Popen.init(
it highlights self._execute_child(
Then in File C:\ProgramData\miniconda3\lib\subprocess.py:1440, in Popen._execute_child(
it highlights _winapi.CreateProcess(

Then it comes to the conclusion
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
FileNotFoundError Traceback (most recent call last)
Cell In[13], line 3
Where in line 3 there is generate_tree_viz([0,1,2,3,4,5,6,7,8,9], y_train, tree)

Then I think is the useful highlight
File ~[my path]\utils.py:185, in generate_tree_viz(root_indices, y, tree)
and the highlight is pos = graphviz_layout(G, prog=“dot”)

And this is what I concluded is possibly the most useful part:
File ~\AppData\Roaming\Python\Python310\site-packages\networkx\drawing\nx_pydot.py:357, in graphviz_layout(G, prog, root)
349 msg = (
350 “nx.nx_pydot.graphviz_layout depends on the pydot package, which has”
351 “known issues and is not actively maintained. Consider using”
352 “nx.nx_agraph.graphviz_layout instead.\n\n”
353 “See Deprecate pydot? · Issue #5723 · networkx/networkx · GitHub
354 )
355 warnings.warn(msg, DeprecationWarning, stacklevel=2)
→ 357 return pydot_layout(G=G, prog=prog, root=root)

File ~\AppData\Roaming\Python\Python310\site-packages\networkx\drawing\nx_pydot.py:412, in pydot_layout(G, prog, root)
408 P.set(“root”, str(root))
410 # List of low-level bytes comprising a string in the dot language converted
411 # from the passed graph with the passed external GraphViz command.
→ 412 D_bytes = P.create_dot(prog=prog)
414 # Unique string decoded from these bytes with the preferred locale encoding
415 D = str(D_bytes, encoding=getpreferredencoding())

File ~\AppData\Roaming\Python\Python310\site-packages\pydot.py:1733, in Dot.init..new_method(f, prog, encoding)
1729 def new_method(
1730 f=frmt, prog=self.prog,
1731 encoding=None):
1732 “”“Refer to docstring of method create.”“”
→ 1733 return self.create(
1734 format=f, prog=prog, encoding=encoding)

File ~\AppData\Roaming\Python\Python310\site-packages\pydot.py:1933, in Dot.create(self, prog, format, encoding)
1930 args = list(e.args)
1931 args[1] = ‘“{prog}” not found in path.’.format(
1932 prog=prog)
→ 1933 raise OSError(*args)
1934 else:
1935 raise

And I get finally my FileNotFoundError: [WinError 2] “dot” not found in path.

Are my imports wrong, do I need to change something in the file, maybe I need to use agraph instead of pydot.
The solution I’ll attempt is to replace in utils
from networkx.drawing.nx_pydot import graphviz_layout
with
from networkx.drawing.nx_agraph import graphviz_layout
And I’ll tell you how it went.

Ok, I did it, now I get the error ImportError: requires pygraphviz http://pygraphviz.github.io/
Restarting JN with that install…

Ok, pygraphviz is a bit challenging for Windows, from that link I’m told to download the stable Win10 2.46.0 version, I did, I added Graphviz to system PATH just in case I need it… It gets downloaded to C:\Program Files\Graphviz …

Ok, so where I am now is I’m trying to install pygraphviz for Windows as mentioned in the Github documentation, the instructions are:
PS C:> python -m pip install --use-pep517 --config-setting="--global-option=build_ext"
–config-setting=“–global-option=”-IC:\Program Files\Graphviz\include" --config-setting="--global-option="-LC:\Program Files\Graphviz\lib"
pygraphviz
and
PS C:> choco install graphviz
PS C:> python -m pip install --use-pep517 --config-setting="--global-option=build_ext"
–config-setting=“–global-option=”-IC:\Program Files\Graphviz\include" --config-setting="--global-option="-LC:\Program Files\Graphviz\lib"
pygraphviz

The choco install fails outright:
Chocolatey installed 0/0 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
Access to the path ‘C:\ProgramData\chocolatey\lib-bad’ is denied.

In the log file, it says
2023-07-16 10:57:45,845 19492 [DEBUG] - Attempting to create directory “C:\ProgramData\chocolatey\lib-bad”.
[…]
2023-07-16 10:57:46,157 19492 [WARN ] - This is try 2/3. Retrying after 400 milliseconds.
Error converted to warning:
Access to the path ‘C:\ProgramData\chocolatey\lib-bad’ is denied.
2023-07-16 10:57:46,572 19492 [ERROR] - Maximum tries of 3 reached. Throwing error.
2023-07-16 10:57:46,606 19492 [ERROR] - Cannot create directory “C:\ProgramData\chocolatey\lib-bad”. Error was:
System.UnauthorizedAccessException: Access to the path ‘C:\ProgramData\chocolatey\lib-bad’ is denied.

As to when I tried installing through python, at first it didn’t like the `s, I replaced them with 's, didn’t work, eliminated them, it doesn’t recognise the path:
Defaulting to user installation because normal site-packages is not writeable
ERROR: Invalid requirement: ‘Files\Graphviz\include --config-setting=–global-option=-LC:\Program Files\Graphviz\lib’
Hint: It looks like a path. File ‘Files\Graphviz\include --config-setting=–global-option=-LC:\Program Files\Graphviz\lib’ does not exist.

The simplified instruction
(base) C:>python -m pip install --use-pep517 pygraphviz
ultimately results in a
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for pygraphviz
Failed to build pygraphviz
ERROR: Could not build wheels for pygraphviz, which is required to install pyproject.toml-based projects

And as a final attempt, I tried re-running with the default files from the course archive, and I’m all the way back to FileNotFoundError: [WinError 2] “dot” not found in path.

And that’s where I gave up as of yet. Is there something I didn’t try which I should have or an alternative I’m not thinking of?

See the MLS FAQ (on the deeplearning.ai site) for tips on how to run the assignments locally.