Failed to execute PosixPath('dot'), make sure the Graphviz executables are on your systems' PATH

I am deploying a Flask application on Koyeb that generates a finite automaton diagram using Graphviz. The app works locally but encounters an issue when deployed on Koyeb.

Environment:

  • Hosting: Koyeb
  • Framework: Flask
  • Graphviz

Problem:

When I send a POST request to the /generate-fa endpoint, I receive the following error:

> {"error":"failed to execute PosixPath('dot'), make sure the Graphviz executables are on your systems' PATH"}

It seems the Graphviz executable is not found in the system’s PATH on the Koyeb instance.

Troubleshooting Steps Taken:

  1. Attempted to install Graphviz using apt-get but encountered sudo command not found issues.
  2. Attempted to install Graphviz locally in the home directory and updated the PATH variable to include the local Graphviz binaries.
  3. Verified that the dot command works correctly locally but not on the deployed instance.

Relevant Code: Here is the code snippet where Graphviz is being called in the Flask app:

def generate_finite_automaton(nodes):
    fa = graphviz.Digraph('finite_automaton', format='png')
    fa.attr(rankdir='LR')

    if nodes.get('o', '') != '' and nodes.get('k', '') != '':
        fa.node('q0', shape='circle', style='filled', fillcolor='yellow')
        fa.node('s', shape='circle', style='filled', fillcolor='yellow')
        fa.node('p', shape='doublecircle', style='filled', fillcolor='yellow')
        fa.node('o', shape='doublecircle', style='filled', fillcolor='yellow')
        fa.node('k', shape='doublecircle', style='filled', fillcolor='yellow')

        fa.edge('q0', 's', label=nodes['s'])
        fa.edge('s', 'p', label=nodes['p'])
        fa.edge('p', 'o', label=nodes['o'])
        fa.edge('o', 'k', label=nodes['k'])

    output_path = 'finite_automaton_custom'
    fa.render(output_path, format='png')

    return f'{output_path}.png'

Request:

How can I properly install Graphviz on Koyeb and ensure that its executables are in the PATH so that my Flask application can use it?

Any guidance on how to resolve this issue or alternative approaches to deploying this Flask app with Graphviz would be greatly appreciated.

Hey!

It seems that Graphviz is a system dependency. Python will not install it for you and we will not either if we are tasked to build Python.

You can fix your issue by bundling it inside a Dockerfile. For example, this should work:

# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

ARG PYTHON_VERSION=3.12.3
FROM python:${PYTHON_VERSION}-slim as base

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1

WORKDIR /app

RUN apt update
RUN apt install -y graphviz

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to requirements.txt to avoid having to copy them into
# into this layer.
RUN --mount=type=cache,target=/root/.cache/pip \
    --mount=type=bind,source=requirements.txt,target=requirements.txt \
    python -m pip install -r requirements.txt

# Copy the source code into the container.
COPY . .

# Expose the port that the application listens on.
EXPOSE 8000

# Run the application.
CMD gunicorn 'app:app' --bind=0.0.0.0:8000

Put this inside a file named Dockerfile at the root of your repository. On Koyeb, choose the “Dockerfile” builder.

Let me know if it helps!

Thank you so much for your solution! Bundling Graphviz inside a Dockerfile as you suggested has fixed my problem perfectly. I really appreciate your help!

1 Like