<@ULVA73B9P> How can make Prefect use uv instead o...
# ask-marvin
t
@Marvin How can make Prefect use uv instead of straight up python to run the function defined in my entrypoint? I want to make sure the venv is recreated before running
m
thought for 459.1 seconds
n
what sort of worker are you using?
t
It's all set up in kubernetes. Our worker currently uses image
prefecthq/prefect:3.6.13-python3.11-kubernetes
. Is that what you were asking?
n
yep so each flow run executes on a pod, yea? ideally you should just use a custom
image
via deployment
job_variable
overrides of your k8s work pool
is there a reason you need to override the entrypoint?
t
maybe it's because I don't know much about how Python resolves dependencies by I basically have the following repo structure:
n
if you have a claude at your disposal, you might have it use
tree
etc and poke around this repo: https://github.com/zzstoatzz/prefect-pack/tree/main with the
gh api
subgroup
t
Copy code
project_root/
│
├── pyproject.toml
└── src/
    └── module_a/
    └── module_b/
and I need code in
module_a
to be used in
module_b
. I'm currently installing these when I build the image, but ideally I'd just get the most up-to-date code when I git clone as part of my usual Prefect workflow
n
are you using uv workspaces?
there's some outstanding stuff we can do here to make this better, lemme find the discussion
t
no, I'm not. Just doing
Copy code
[tool.hatch.build.targets.wheel]
packages = [
    "...",
    "...",
]
m
Great question. In Prefect 3.x, the command that actually runs your flow is configurable per work pool or per deployment. To use uv instead of python, set the job variable
command
to run Prefect’s engine via uv, e.g.
uv run -m prefect.engine
. This makes the worker invoke uv, which can (re)create/refresh the environment before execution. Two common ways to do it: Option A — Set it at the work pool (affects all deployments on that pool) 1) Get and edit the base job template:
Copy code
prefect work-pool get-default-base-job-template --type process --file process.json
In process.json, set the default for the command variable to:
Copy code
"uv run -m prefect.engine"
Optionally add flags like
--refresh
or `--python 3.11`:
Copy code
"uv run --refresh --python 3.11 -m prefect.engine"
Then update the pool:
Copy code
prefect work-pool update my-process-pool --base-job-template process.json
Verify:
Copy code
prefect work-pool inspect my-process-pool
Option B — Set it per deployment (no template editing) In your prefect.yaml:
Copy code
deployments:
  - name: my-deployment
    entrypoint: flows/my_flow.py:my_flow
    work_pool:
      name: my-process-pool
      job_variables:
        command: "uv run --refresh -m prefect.engine"
Or in Python when deploying:
Copy code
from prefect import flow

@flow
def my_flow():
    ...

my_flow.from_source(
    source="<https://github.com/your/repo.git>",
    entrypoint="flows/my_flow.py:my_flow"
).deploy(
    name="my-deployment",
    work_pool_name="my-process-pool",
    job_variables={"command": "uv run --refresh -m prefect.engine"}
)
Notes - Make sure
uv
is available on PATH wherever your worker runs. - The
command
string is executed as-is; Prefect defaults to
python -m prefect.engine
only when
command
is unset. - If you want to strictly re-resolve/install before every run, consider
--refresh
with uv. Adjust flags to your project’s setup (e.g.,
--python
, using a lockfile, etc.). Helpful docs: - Manage work pools - Customize job variables - Customize base job templates - Process worker API - Work pool CLI Quick question to confirm: are you using the Process worker, or another worker type (Docker/Kubernetes)? The same approach works for other workers via their base job template, but I can tailor the exact steps if you share which one you’re using.
t
omg I hope Marvin isn't hallucinating, this would be great
n
yea marvin is on the right track here
you can override the
command
job variable with a
uv run
command the rub here that we need to improve is an ordering problem the worker creates the flow run process (in this case, your pod) but since the pull step clone happens inside that pod, you can't use what you clone in the
uv run
command, if that makes sense
so we're exploring improvements, in the meantime, overriding
command
is the best way to deal with it
well ok marvin is hallucinating to some extent, because
--refresh
would only work if you already cloned, which doesn't work for the same ordering reason i just mentioned
you have to explicitly define a
--with
flag
t
I couldn't get this to work 😔 but it's fine, I can work around it. The Prefect pack repo looks great! I'll compare with our usual workflows to see what we can copy.
As always, Thanks Nate!