I'm looking for the way to get a flow to run with ...
# ask-community
w
I'm looking for the way to get a flow to run with other classes, which are defined in a separate file. The idea being that we can encapsulate business logic in a class that multiple flows can refer to. We're not looking to create an installed package, just host the class file alongside the flow file. Is that able to be done? Just importing the class gives the flow error: "Failed to load and execute Flow's environment: FlowStorageError('An error occurred while unpickling the flow:\n ModuleNotFoundError("No module named \'doMath\'")\nThis may be due to a missing Python module in your current environment. Please ensure you have all required flow dependencies installed.')"
a
@Will List it’s mainly a DevOps/packaging issue and it depends on your setup. What agent do you use? If you use some Docker-based agent (Docker, Kubernetes, ECS), then there is really no other way but to package it into a Docker image in some way - either via installable package or moving the modules into your image and ensuring they are added to PYTHONPATH. But if you’re using Local agent, this is doable as long as your agent and flow’s working directory have access to your local classes.
This repo has an example showing how you could use custom modules in your flows and shows how those modules can be packaged and accessed from a flow: https://github.com/anna-geller/packaging-prefect-flows
w
Thank you for the pointers, Anna 🙂
👍 1
I'm running a local agent, but as seen in https://github.com/anna-geller/packaging-prefect-flows/blob/master/flows/local_storage_local_run_explicit_custom_modules.py , the code still requires the additional files 'installing' in the environment, rather than relying on python's native ability to read the directory the script is in. With that confirmed, I'm going to leave this one for posterity, and 'install' my additional files.
a
@Will List you can provide a working directory to LocalRun, which will allow to access local modules
e.g. to start the agent:
Copy code
prefect agent local start -p /Users/will/custom_package/
and on the flow:
Copy code
flow.run_config = LocalRun(working_dir="/Users/will/custom_package/")
upvote 2