Comments (11)
In light of @sepharg's suggestion, using debugpy which is the replacement of ptvsd(for VSCode users, or Vim/Emacs+DAP users), adapted to the current structure of the project:
-
Add the following to the settings at
backend/app/core/config.py
:DEBUG_MODE: bool = False WAIT_FOR_ATTACH: bool = False
-
Add the following dev depenency to
backend/app/pyproject.toml
, under[tool.poetry.dev-dependencies]
:
debugpy = "^1.2"
-
Add the following code to
backend/app/main.py
, right before theFastAPI
constructor:if settings.DEBUG_MODE: import debugpy debugpy.listen(("0.0.0.0", 5432)) if settings.WAIT_FOR_ATTACH: debugpy.wait_for_client()
Optionally(not related to the debugger), add the argument
debug=settings.DEBUG_MODE
to theFastAPI
constructor to have stacktraces upon API endpoint errors. -
Add the port mapping
- "5432:5432"
(or any other external port you wish) to
docker-compose.override.yml
underservices.backend.ports
-
Add
DEBUG_MODE=True
to the.env
file and optionallyWAIT_FOR_ATTACH=True
At this point, you should be able to connect to localhost:5432
via any debugger that supports DAP
For VSCode users, make the backend/app/.vscode/launch.json
as follows: (assuming you open the VSCode editor at the backend/app
folder)
{
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5432
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
]
}
]
}
This has worked for me, using Docker on Windows (WSL2 engine) and VSCode
from full-stack-fastapi-template.
Hello!
@tiangolo, i try instruction by @LukGerman, but can't debug backend via pycharm.
I need to put breakpoint to code,
Is it possible?
do you help me?
Thanks a lot!
from full-stack-fastapi-template.
For pycharm i got it working by doing this:
- Set up a remote interpreter: https://www.jetbrains.com/help/pycharm/using-docker-compose-as-a-remote-interpreter.html which is connected to the
backend
service - Add a file called
run.py
next tomain.py
import uvicorn
if __name__ == "__main__":
uvicorn.run(
"main:app",
host="0.0.0.0",
port=8567,
log_level="debug",
reload=True,
workers=1
)
- Add a pycharm configuration to run that file. Select your local
main.py
file and the remote python interpreter you created in step 2. Make sure the working directory isbackend/app
- Modify
docker-compose-override.yml
to expose backend port8567
, by adding the following line- "8567:8567"
under- "8888:8888"
. - Modify your frontend to talk to that port, by adding
:8567
toenvApiUrl
fordev
inenv.ts
. (Skip this step if you don't need the frontend). - Add CORS for that port, by adding it to your
.env
.
Then to run it:
docker-compose up
(as usual)- run or debug your configuration from step 3. It should now kill the backend service created by docker-compose and replace it with its own.
If anyone knows more about traefik and docker, there is probably an easier way that doesn't require me to add a new backend port and hardcode it in the frontend. Please tell me if there is :)
from full-stack-fastapi-template.
I cannot help with VSCode, but I'd like to leave an instruction on how to do the same thing when using Pycharm. To debug backend app deployed locally using docker-compose, my preferred way is to use Pycharm's remote debugger.
Here is a documentation on how to set it up.
Few caveats that require a little explanation when debugging project deployed in a local docker environment:
- In a 'run/debug configuration' window, as a Local host name use
localhost
. When creating a mapping, use the same mapping that is defined indocker-compose.dev.volumes.yml
, which is<path in your host filesystem>/backend/app=/app
. - Installation of the
pydevd-pycharm~=<version of PyCharm on the local machine>
can be added inbackend/backend.dockerfile
, just append"pydevd-pycharm~=193.6494"
at the end of the pip install run. Version can be checked using Pycharm's menu bar -> help -> about. - Finally, add
import pydevd_pycharm pydevd_pycharm.settrace('172.22.0.1', port=12345, stdoutToServer=True, stderrToServer=True)
inbackend/app/app/main.py
. Replace IP address, with an address of your host. It can be checked usingdocker inspect <container-id-or-name> | grep Gateway
(e.g.docker inspect projectslug_proxy_1 | grep Gateway
).
It would be even better to use some env var to enable debugging only in development.
Now you can run your debugger, thendocker-compose up
and voila!
from full-stack-fastapi-template.
Thanks for the report, I'll take a look into this.
from full-stack-fastapi-template.
It seems the solution is quite simple: one can add the following lines in main.py:
import ptvsd
ptvsd.enable_attach(redirect_output=True)
and then use such VSCode launch.json config as:
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}/backend/app",
"remoteRoot": "."
}
]
}
from full-stack-fastapi-template.
I've tried this approach and it's not working for me, at least using the latest ptvsd
i can connect to the debugger but breakpoints aren't hit
any help would be appreciated!
main.py
import ptvsd
from flask import Flask
app = Flask(__name__)
ptvsd.enable_attach(log_dir='/var/log/')
@app.route("/")
def hello():
return "Hello World from Flask!"
if __name__ == "__main__":
# Only for debugging while developing
app.run(host='0.0.0.0', debug=True, port=80)
launch.json (i'm on windows)
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}\\app",
"remoteRoot": "/app"
}
]
}
Dockerfile
FROM tiangolo/uwsgi-nginx-flask:python3.6
COPY ./app /app
ADD requirements.txt /app/
WORKDIR /app
RUN pip install -r requirements.txt
ENV PTVSD_DEBUG 1
requirements.txt
ptvsd
from full-stack-fastapi-template.
What's up with this @tiangolo ?
This project is great, but not knowing how to debug it makes development a tedious headache.
from full-stack-fastapi-template.
@nmdanny provided excelent instructions, unfortunately a typo on the port number, 5432 is for postgres, they should have been 5678, the default port for debugpy.
Another minor issue is in the pathmappings, if the IDE workspace, is the project root folder, should use:
"localRoot": "${workspaceFolder}/backend/app",
"remoteRoot": "."
from full-stack-fastapi-template.
@nmdanny's setup works for me - using docker on Ubuntu 20.04
from full-stack-fastapi-template.
Thanks @nmdanny and @okz for your solutions. It works for me.
Question for @okz :
Regarding 5432 being the default port for postgres -
Aren't we using port 5050 for postgres for this project? Why is it blocked then?
I'm newbie here. Sorry if the question is too naive
from full-stack-fastapi-template.
Related Issues (20)
- Deploying on a machine with nginx HOT 1
- Why React, not Vue?
- Mac M1 Docker error HOT 6
- Bad Gateway returned HOT 5
- [Improvement] Replace Prettier and Eslint with Biome and add pre-commit hooks HOT 1
- Feature Request: Support for GitHub Codespaces Deployment and Development HOT 2
- Login error in Dashboard HOT 5
- Secrets should not be versioned by default HOT 3
- Enable discussion on this repo? HOT 1
- Support the use of dev containers for reproducible dev environment HOT 1
- Embracing Technological Evolution: A Journey Toward Innovation
- Debug Launch Configuration for "Debug Frontend: (...)" does not work on WSL
- Z
- Running task 1 of 1: python .copier/update_dotenv.py /bin/sh: 1: python: not found HOT 8
- How to start the backend server without using docker?
- FASTAPI
- 🐛 Error in frontend when resetting a password: "The passwords do not match" HOT 1
- Sorry, please ignore.
- backend not working HOT 1
- Titel: "Implementiere AutoGen Base Agent"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from full-stack-fastapi-template.