How to Debug Mini Course Code Using VS Code ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can use the VS Code Debug mode and breakpoints with the following configuration. .. warning:: You will need to disable the caching to hit the breakpoint for some parts of the code. 1. Override the Container Command --------------------------------- First, override the **command** and the **ports** fields in the **docker-compose** file for the service you would like to test. You can use the `docker-compose.override.yml` file to achieve this, it is located at :code:`"$(tutor config printroot)/env/dev/docker-compose.override.yml"`. The **command** should be replaced with the following: .. code-block:: yaml command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 ./manage.py lms runserver 0.0.0.0:8000"] The 5678 port is the debugging port and 8000 port is the default lms port. You should also replace lms with cms if you are testing the cms. I am using `debugpy` as it is the default suggestion from VS Code. The **ports** should be replaced with the following: .. code-block:: yaml ports: - "8000:8000" - "5678:5678" It will look something like this in the end: .. code-block:: yaml lms: volumes: - "{REMOVED}/tutor/volumes/edx-platform:/openedx/edx-platform" command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 ./manage.py lms runserver 0.0.0.0:8000 --nothreading"] ports: - "8000:8000" - "5678:5678" 2. Configure VS Code -------------------- You should create a `launch.json` file under the `.vscode` folder in your workspace root. I have the following structure, you should adjust the following configuration accordingly if you have a different structure. :: - root - `${workspaceFolder}` - /tutor - `$(tutor config printroot)` - /env - /data - /volumes - /tutor-plugins You can use the following configuration, however, you should change the port and the localRoot if you have changed the default for any of those. .. code-block:: json { "configurations": [ { "name": "Python: Remote Attach", "type": "python", "request": "attach", "connect": { "host": "localhost", "port": 5678 }, "pathMappings": [ { "localRoot": "${workspaceFolder}/tutor/volumes/edx-platform", "remoteRoot": "/openedx/edx-platform" } ] } ] } 3. Start Developing ------------------- You can use the familiar :code:`tutor dev start -d lms` and :code:`tutor dev stop lms`. The LMS will not launch until the debugger is attached. Everything above applied to CMS as well, just change lms with cms. References ---------- * manage.py - https://docs.djangoproject.com/en/4.1/ref/django-admin/ * VS Code Debug Docker Compose - https://code.visualstudio.com/docs/containers/docker-compose * Tutor docker-compose.override.yml - https://docs.tutor.overhang.io/dev.html?highlight=override#override-docker-compose-volumes * Original post - https://discuss.openedx.org/t/how-to-debug-python-code-using-vscode/8111/5?u=uetuluk