fgmacedo / python-statemachine

Python Finite State Machines made easy.
MIT License
854 stars 84 forks source link

Failed to generate a graphic image #423

Closed dg-deus closed 6 months ago

dg-deus commented 6 months ago

Description

I installed python-statemachine, pydot, Graphviz seperately on PyCharm, and I copied and ran the code of TrafficLightMachine. I tried to generate a graphic image of it following the instruction in Getting started. However, I keep having the FileNotFoundError: [WinError 2] "dot" not found in path.

What I Did

I ran the following code on PyCharm Python Console.

from statemachine import StateMachine, State

class TrafficLightMachine(StateMachine):
    "A traffic light machine"
    green = State(initial=True)
    yellow = State()
    red = State()

    cycle = (
        green.to(yellow)
        | yellow.to(red)
        | red.to(green)
    )

    def before_cycle(self, event: str, source: State, target: State, message: str = ""):
        message = ". " + message if message else ""
        return f"Running {event} from {source.id} to {target.id}{message}"

    def on_enter_red(self):
        print("Don't move.")

    def on_exit_red(self):
        print("Go ahead!")

sm = TrafficLightMachine()
sm._graph().write_png("./fsm-test.png")

And got the following error.

Traceback (most recent call last):
  File "C:\work\python\venv\lib\site-packages\pydot\core.py", line 1753, in create
    stdout_data, stderr_data, process = call_graphviz(
  File "C:\work\python\venv\lib\site-packages\pydot\core.py", line 133, in call_graphviz
    process = subprocess.Popen(
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2023.2.3\plugins\python\helpers\pydev\pydevconsole.py", line 364, in runcode
    coro = func()
  File "<input>", line 59, in <module>
  File "C:\work\python\venv\lib\site-packages\pydot\core.py", line 1587, in new_method
    self.write(path, format=f, prog=prog, encoding=encoding)
  File "C:\work\python\venv\lib\site-packages\pydot\core.py", line 1662, in write
    s = self.create(prog, format, encoding=encoding)
  File "C:\work\python\venv\lib\site-packages\pydot\core.py", line 1762, in create
    raise OSError(*args)
FileNotFoundError: [WinError 2] "dot" not found in path.
tolstovdmit commented 6 months ago

Hi, on line 1762 pydot tries to call dot.exe and looking for it in PATH environment variable. You probably need to add a path for Graphviz to PATH. Mine, for example, contains "C:\Program Files\Graphviz\bin"

dg-deus commented 6 months ago

I found dot.ext file in the same path after installing Graphviz from its website, and now it works. Thank you.