Skip to content

fix(cli): Run Python apps with the project venv in local run - #2349

Merged
MathurAditya724 merged 5 commits into
mainfrom
fix/local-run-python-detection
Nov 18, 2026
Merged

MathurAditya724 merged 5 commits into
mainfrom
fix/local-run-python-detection

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

sentry local run with no command picked the wrong command for most Python projects.

It used the system Python even when the project had a .venv that wasn't activated, so the app failed with ModuleNotFoundError. It also ran FastAPI and Flask apps as python main.py. A normal app file has no __main__ block, so that exits right away and the CLI stops without saying why. A Flask app in wsgi.py, or one picked with FLASK_APP, wasn't detected at all.

Now it uses the venv interpreter when there is one (VIRTUAL_ENV, then .venv/, then venv/), even when it isn't activated. A top-level app = FastAPI() runs with uvicorn and app = Flask(__name__) with flask --app <module>:<name> run. A top-level app = create_app() uses whichever of the two the project's dependencies list. Anything else, or a file with a __main__ block, still runs as python <file>, and Django still runs manage.py runserver.

FLASK_APP comes after app.py, main.py and wsgi.py, and only counts in a folder that looks like a Python project. So one left in your shell profile won't take over a Go or Compose project. The order is now: package.json, manage.py, app.py, main.py, wsgi.py, FLASK_APP, go.mod, compose.

Before (FastAPI main.py, .venv not activated):

Detected main.py: python3 main.py
ModuleNotFoundError: No module named 'sentry_sdk'

After:

Detected main.py (FastAPI): .venv/bin/python -m uvicorn main:app --reload
Uvicorn running on http://127.0.0.1:8000

This uses uvicorn, not fastapi dev. Plain pip install fastapi adds a fastapi script that fails unless fastapi[standard] is installed, and every setup where fastapi dev works has uvicorn too. flask --app needs Flask 2.2 or later.

via @MathurAditya724.

--

View Session [Sentry]

With no command, `sentry local run` ran the system Python even when the
project had an unactivated `.venv` or `venv`, so imports failed. It also ran
FastAPI and Flask apps as `python main.py`, which exits right away when the
file has no `__main__` block.

Use the venv interpreter (`VIRTUAL_ENV`, then `.venv/`, then `venv/`) when
there is one. Serve a FastAPI app with `python -m uvicorn <module>:app
--reload` and a Flask app with `python -m flask --app <module> run`, found
from the app in app.py or main.py or the declared dependency. Also detect
`FLASK_APP` and a Flask `wsgi.py`. A file with a `__main__` block still runs
as a script.

Co-Authored-By: Aditya Mathur <[email protected]>
@vercel

vercel Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
sentry-local Skipped Skipped Nov 18, 2026 7:48am UTC

Request Review

Comment thread packages/cli/src/lib/dev-script.ts Resolved
Comment thread packages/cli/src/lib/dev-script.ts Resolved
Comment thread packages/cli/src/lib/dev-script.ts Resolved
The app in app.py or main.py now decides the framework before the
declared dependencies do, so a Flask app in a project that also lists
FastAPI runs with flask. Only count a dependency at the start of a line
or a quoted string, not in comments or prose. Run a wsgi.py with a
__main__ block as a script instead of skipping it.

Co-Authored-By: Aditya Mathur <[email protected]>
sentry-junior Bot and others added 2 commits November 18, 2026 07:43
Only honor FLASK_APP in a Python project, after app.py, main.py and
wsgi.py, so one exported from a shell profile no longer takes over a Go,
Compose, or FastAPI project.

Use the declared dependency only when the file assigns a top-level `app`
or `application`; a file that imports FastAPI for typing, or a factory
with no top-level app, runs as a script again. Match `Flask(` only as a
top-level assignment and pass its variable to `--app module:name`. Read
pyproject.toml dependencies only from dependency arrays and Poetry
dependency tables, so `keywords = ["flask"]` does not count.

Co-Authored-By: Aditya Mathur <[email protected]>
@sentry-junior
sentry-junior Bot marked this pull request as ready for review November 18, 2026 08:04
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 18, 2026

@MathurAditya724 MathurAditya724 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed Python venv selection, framework and factory detection, command precedence, Windows paths, and dependency-file parsing. All 52 unit/property tests pass; also verified a real Flask server starts from the detected project .venv without activation and serves an HTTP request. GitHub CI passes; no blocking findings.

@MathurAditya724
MathurAditya724 merged commit 02da965 into main Nov 18, 2026
31 checks passed
@MathurAditya724
MathurAditya724 deleted the fix/local-run-python-detection branch November 18, 2026 10:23

This branch was previously deployed

1 inactive deployment
Preview — d93f1a96 Deployed Nov 18, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant