skip to Main Content

I am fairly new to VS Code (IDE) and Python (I have plenty of experience with Visual Studio and C#). I have written the following Hello World type program (test.py) using VS Code:

import sys

def main():
    print(sys.argv)
    print(sys.argv[1])
    print(sys.argv[2])
    print("hello world")

if __name__ == "__main__":
    main()

The Launch.json looks like tis:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": [parameter1", "parameter2"]
        }
    ]
}

If I select Run/Start Debugging then everything works as expected i.e. the arguments passed are printed to the terminal in VS Code. If I add a breakpoint then the code stops as expected at the breakpoint.

I have come across this webpage and specifically the "Command line debugging section" (https://code.visualstudio.com/docs/python/debugging). I tried this (as stated on the webpage):

  1. Open terminal

  2. Type: python -m pip install --upgrade debugpy

  3. Press enter

  4. Amend the launch.json to this:

    {
    "name": "Python Debugger: Attach",
    "type": "debugpy",
    "request": "attach",
    "connect": {
    "host": "localhost",
    "port": 5678
    },
    "args": [parameter1", "parameter2"] }

  5. If I Run/Start debugging at this stage then everything still works as expected i.e. the arguments passed are printed to the terminal in VS Code and the code stop as expected at the breakpoint.

  6. Type: python -m debugpy --listen 0.0.0.0:5678 ./test.py

The code errors with this:

0.01s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
['./test.py']
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:UsersadminAppDataLocalProgramsPythonPython312Libsite-packagesdebugpy__main__.py", line 39, in <module>
    cli.main()
  File "C:UsersadminAppDataLocalProgramsPythonPython312Libsite-packagesdebugpyservercli.py", line 430, in main
    run()
  File "C:UsersadminAppDataLocalProgramsPythonPython312Libsite-packagesdebugpyservercli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "C:UsersadminAppDataLocalProgramsPythonPython312Libsite-packagesdebugpy_vendoredpydevd_pydevd_bundlepydevd_runpy.py", line 321, in run_path        
    return _run_module_code(code, init_globals, run_name,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersadminAppDataLocalProgramsPythonPython312Libsite-packagesdebugpy_vendoredpydevd_pydevd_bundlepydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:UsersadminAppDataLocalProgramsPythonPython312Libsite-packagesdebugpy_vendoredpydevd_pydevd_bundlepydevd_runpy.py", line 124, in _run_code       
    exec(code, run_globals)
  File "./test.py", line 10, in <module>
    main()
  File "./test.py", line 5, in main
    print(sys.argv[1])
          ~~~~~~~~^^^
IndexError: list index out of range

If I comment out the following lines, then the code runs from the command line, however breakpoints are not honoured:

print(sys.argv)
print(sys.argv[1])
print(sys.argv[2])

My two concerns are:

  1. Why can’t I provide command line arguments when command line debugging? It works if you pass the aruements via a terminal command like this: python -m debugpy --listen 0.0.0.0:5678 ./test.py "parameter1","parameter2". It appears the args in the launch.json are ignored.
  2. Why aren’t breakpoints honoured when command line debugging?

I have tried adding the -Xfrozen_modules=off to help with the breakpoint issue (as suggested by the terminal message above), however it makes no difference. I have also tried adding the --wait-for-client flag (mentioned in the link I provided) to help with the breakpoint issue, however this also made no difference.

I am using Visual Studio Code 1.86, debugpy 24.077.775 and Python 3.12.3. Windows 10 64 bit.

2

Answers


  1. In your launch.json, it seems you are missing a quotation mark before the parameter1 causing the args to not be read correctly.

            "args": [parameter1", "parameter2"]
    
    Login or Signup to reply.
  2. After setting up the server with the –wait-for-client flag and the above configuration, start the debugging session in VS Code using the "Attach to Remote" configuration.

    If breakpoints are still not being respected, ensure that:

    Your source code on the VS Code side matches exactly with what’s running on the debug server.
    There are no path discrepancies that might be causing VS Code to not match the source files correctly.

    Make sure you use the -Xfrozen_modules=off flag only if you are experiencing issues related to Python’s optimizations around frozen modules.
    Double-check that you have the latest versions of VS Code, Python, and the debugpy library, as updates can often fix bugs related to debugging features.
    Finally, verify your entire setup and experiment with different configurations to pinpoint the exact cause. For more detailed guidance on using debugpy, consult the official debugpy documentation and the VS Code debugging guide, which you’re already familiar with.

    https://github.com/microsoft/debugpy

    https://code.visualstudio.com/docs/python/debugging

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search