How to Debug python code using vscode

Hello everyone, I’m beginner with openedx. There are some codes that I need to debug to view some variables but I really don’t know how to debug in vscode (do I need create file launch.json). Is there any way when I direct url , break point stop at place I marked before in python file

image

Please help me , Thanks

I am also interested in the solution. Please do share if you figure it out.

Hello there! This sounds quite insteresting, although I’ve never invested a lot of time into this kind of configuration since, at least for me, adding a breakpoint() or import pudb; pu.db in the code and then attaching a terminal to the running container (whichever service that is).

Either way, I hope this thread has some resolution since I’m also interested :sweat_smile:

Hello @luan.tm. Like @mgmdi, we run a containerized implementation of Open edX and use a mounted volume. As a result, there is no way to evaluate the running code directly without entering the container (or at least attaching a debugger to it somehow).

Our solution is to either dump the data to the output and view it using something like tutor dev logs -f --tail 50 lms cms learning or create a context['debug'] field to write the debug data or variables to the browser window.

You might consider using the Tutor implementation to use ipdb.set_trace (Open edX development — Tutor documentation). I haven’t actually tried setting this up because dumping values to the console or webpage has been good enough for our needs.

@luan.tm

I have done some experimentation and you can use the VS Code Debug mode and breakpoints with the following configuration.

However, 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 "$(tutor config printroot)/env/dev/docker-compose.override.yml".

The command should be replaced with the following:

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:

ports:
- "8000:8000"
- "5678:5678"

It will look something like this in the end:

  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.

{
    "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 tutor dev start -d lms and 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

5 Likes