Python venv created in Build phase not available in Deploy phase
lp0408165x-cloud
HOBBYOP

2 months ago

I'm deploying a FastAPI application using nixpacks.toml configuration.

## Issue

Virtual environment created during build phase is not available at runtime.

## Build phase (successful):

- python -m venv /app/venv

- /app/venv/bin/pip install -r backend/requirements.txt

## Deploy phase (fails):

- Error: /bin/bash: line 1: uvicorn: command not found

- Start command: cd backend && /app/venv/bin/uvicorn app.main:app --host 0.0.0.0 --port $PORT

## My nixpacks.toml:

```toml

[phases.setup]

nixPkgs = ["python311"]

[phases.install]

cmds = [

"python -m venv /app/venv",

"/app/venv/bin/pip install -r backend/requirements.txt"

]

[start]

cmd = "cd backend && /app/venv/bin/uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-8000}"

```

## Question

How can I ensure the venv directory created during build is preserved and available during deploy/runtime?

GitHub repo: https://github.com/lp0408165x-cloud/gtc-ai-platform

$10 Bounty

1 Replies

domehane
FREE

2 months ago

nixpacks automatically creates a venv at /opt/venv, not /app/venv. your custom venv isn't persisting

so you should change your nixpacks.toml to this:

[phases.setup]
nixPkgs = ["python311"]

[phases.install]
cmds = ["pip install -r backend/requirements.txt"]

[start]
cmd = "cd backend && uvicorn app.main:app --host 0.0.0.0 --port $PORT"

remove the manual venv creation entirely. nixpacks handles it automatically and adds uvicorn to PATH, so you can just call uvicorn directly without the full path.

alternatively if you want to keep your structure, just change the start command to use /opt/venv/bin/uvicorn instead of /app/venv/bin/uvicorn


Loading...