Tutorial: Attaching Visual Studio Code to Devstack LMS Container

@viadanna Have you tried debugging an individual unit test with VS Code for the LMS? I’m able to run the pytest lms/<path>/test_*.py successfully, however, I’m not able to debug to see why that test is failing. Any advice on how to do this with VS Code?

Running LMS and Studio Tests

Hey @Zachary_Trabookis

To be honest, I never invested the time for debugging tests in VSCode given the current issues we are facing.

I usually resort to

$ pytest -x --pdb <test_file>

ref.

Thanks for the response @viadanna. Yeah I’m resorting to pytest <test_file> too. Thanks for the optional parameters you mentioned above.

Maybe one day someone will figure out how to run tests within VS Code but that edX command pytest works for now along with pdb.

PTVSD has an issue with the address being in use if you restart:

So instead of normal attach, we can use this,

Thanks a lot for the solution to our woes @farhaanbukhsh :clap::clap::clap:

I can confirm it works when replacing the original code in private.py with

import os
import ptvsd

if os.environ.get('RUN_MAIN') or os.environ.get('WERKZEUG_RUN_MAIN'):
    ptvsd.enable_attach(address=('0.0.0.0', 5678), redirect_output=True)

I’ve updated the original blog post that originated this at https://opencraft.com/blog/tutorial-attaching-visual-studio-code-to-devstack-lms-container/

@viadanna I got an issue when trying to debug a third party python package and wanted to let you know how I got around that. It wouldn’t let me debug it.

Add this within your launch.json file. It gives you a warning about property not being allowed but it works. I found the solution here https://github.com/microsoft/ptvsd/issues/2103#issuecomment-596475875

{
    "version": "0.2.0",
    "configurations": [
        {
            ...
            "debugStdLib": true
        }
    ]
}
1 Like

Nice find, I’ll keep that in mind and add to our blog post :+1:

@viadanna If you’re looking to debugging a Django management command follow these steps. This may be worth adding your blog post on how to debug a management command.

Update: I’m able to get the debugger starting to work with the following ${workspaceFolder}/vscode/launch.json configuration. The file below is running the refresh_course_metadata Django management command from the course-discovery service.

You can either update the default ${workspaceFolder}/.env file with the environment variables or add the “env” to the launch configuration as shown below. I added in the virtual environment variables from /edx/app/discovery/discovery_env settings.

The ${workspaceFolder}/.env is the default Python: Env File setting for the workspace.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Remote Attach to Course Discovery Container - refresh_course_metadata Django management command",
            "type": "python",
            "request": "launch",
            "django": true, // allows you to set breakpoints in Django template files.
            "subProcess": true,
            "debugStdLib": true,
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "refresh_course_metadata",
                "--partner_code",
                "edx",
                "--settings",
                "course_discovery.settings.devstack",
            ],
            "python": "/edx/app/discovery/venvs/discovery/bin/python",
            "env": {
                "PATH": "/edx/app/discovery/nodeenvs/discovery/bin:/edx/app/discovery/venvs/discovery/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin",
                "DJANGO_SETTINGS_MODULE": "course_discovery.settings.devstack",
                "DISCOVERY_CFG": "/edx/etc/discovery.yml",
            }
        }
    ]
}

Hey @Zachary_Trabookis, it seems that you were able to get it working. I took a look at my own launch for manage.py and it doesn’t look too different.

I noticed that my own has some environment variables missing but still works.

One question, do you need to use the debugStdLib? My VScode complains that the property is not allowed but I see it working when argparse throws an exception.

Here’s what I use:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Launch",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/edx-platform/manage.py",
            "args": [
                "lms",
                "dump_course_ids",
            ],
            "python": "${workspaceFolder}/venvs/edxapp/bin/python",
            "env": {
                "LMS_CFG": "/edx/etc/lms.yml",
            },
            "django": true,
            "subProcess": true
        }
    ]
}

@viadanna Thanks for sharing your configuration for Django management for LMS dump_course_ids. That looks similar to what I did with refresh_course_metadata for the course-discovery service.

I would keep debugStdLib in the configuration since it allows you to debug third-party libraries. That is in regards to this response.