Repository navigation
fix(cli): Run Python apps with the project venv in local run - #2349
Merged
Merged
Conversation
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]>
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
Co-Authored-By: Aditya Mathur <[email protected]>
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]>
…detection Co-Authored-By: Aditya Mathur <[email protected]>
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]>
MathurAditya724
approved these changes
Nov 18, 2026
MathurAditya724
left a comment
Member
There was a problem hiding this comment.
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.
7 tasks done
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
sentry local runwith no command picked the wrong command for most Python projects.It used the system Python even when the project had a
.venvthat wasn't activated, so the app failed withModuleNotFoundError. It also ran FastAPI and Flask apps aspython 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 inwsgi.py, or one picked withFLASK_APP, wasn't detected at all.Now it uses the venv interpreter when there is one (
VIRTUAL_ENV, then.venv/, thenvenv/), even when it isn't activated. A top-levelapp = FastAPI()runs with uvicorn andapp = Flask(__name__)withflask --app <module>:<name> run. A top-levelapp = create_app()uses whichever of the two the project's dependencies list. Anything else, or a file with a__main__block, still runs aspython <file>, and Django still runsmanage.py runserver.FLASK_APPcomes afterapp.py,main.pyandwsgi.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,.venvnot activated):After:
This uses uvicorn, not
fastapi dev. Plainpip install fastapiadds afastapiscript that fails unlessfastapi[standard]is installed, and every setup wherefastapi devworks has uvicorn too.flask --appneeds Flask 2.2 or later.via @MathurAditya724.
--
View Session [Sentry]