# How to Debug python code using vscode

**URL:** <https://discuss.openedx.org/t/how-to-debug-python-code-using-vscode/8111>\
**Category:** Architecture\
**Tags:** how-to\
**Created:** [August 18, 2022, 6:26am UTC](https://discuss.openedx.org/t/how-to-debug-python-code-using-vscode/8111 "2022-08-18T06:26:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luan.tm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/luan.tm/32/4138_2.png) [@luan.tm](https://discuss.openedx.org/u/luan.tm)\
**Post date:** [August 18, 2022, 6:26am UTC](https://discuss.openedx.org/t/how-to-debug-python-code-using-vscode/8111/1 "2022-08-18T06:26:01Z")

</div>

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](https://us1.discourse-cdn.com/flex020/uploads/openedx/original/2X/9/9a22d28ee70aa5dc8cfd4a9cff3e8050aefb76b6.png)

Please help me , Thanks

---

<div class="post-metadata">

**Author:** ![uetuluk](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/uetuluk/32/3015_2.png) [@uetuluk](https://discuss.openedx.org/u/uetuluk)\
**Post date:** [August 19, 2022, 4:51am UTC](https://discuss.openedx.org/t/how-to-debug-python-code-using-vscode/8111/2 "2022-08-19T04:51:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mgmdi](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/mgmdi/32/1684_2.png) [@mgmdi](https://discuss.openedx.org/u/mgmdi)\
**Post date:** [August 19, 2022, 6:11pm UTC](https://discuss.openedx.org/t/how-to-debug-python-code-using-vscode/8111/3 "2022-08-19T18:11:05Z")

</div>

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 😅

---

<div class="post-metadata">

**Author:** ![TonyH](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/tonyh/32/4079_2.png) [@TonyH](https://discuss.openedx.org/u/TonyH)\
**Post date:** [August 20, 2022, 6:12am UTC](https://discuss.openedx.org/t/how-to-debug-python-code-using-vscode/8111/4 "2022-08-20T06:12:18Z")

</div>

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](https://docs.tutor.overhang.io/dev.html)). I haven’t actually tried setting this up because dumping values to the console or webpage has been good enough for our needs.

---

<div class="post-metadata">

**Author:** ![uetuluk](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/uetuluk/32/3015_2.png) [@uetuluk](https://discuss.openedx.org/u/uetuluk)\
**Post date:** [August 22, 2022, 7:14am UTC](https://discuss.openedx.org/t/how-to-debug-python-code-using-vscode/8111/5 "2022-08-22T07:14:39Z")

</div>

@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:

```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:

```yaml
ports:
- "8000:8000"
- "5678:5678"

```

It will look something like this in the end:

```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.

```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 `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

- manage.py - [django-admin and manage.py | Django documentation | Django](https://docs.djangoproject.com/en/4.1/ref/django-admin/)
- VS Code Debug Docker Compose - [Use Docker Compose](https://code.visualstudio.com/docs/containers/docker-compose)
- Tutor docker-compose.override.yml - [Open edX development — Tutor documentation](https://docs.tutor.overhang.io/dev.html?highlight=override#override-docker-compose-volumes)
