vscode 调试python内置库断不下来的问题

原文链接: https://my.oschina.net/u/1777508/blog/1635054

为什么80%的码农都做不了架构师?>>> hot3.png

    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${file}",
            "cwd": "${workspaceFolder}",
            "env": {},
            "envFile": "${workspaceFolder}/.env",
            "debugOptions": [
                "RedirectOutput",
                "DebugStdLib"
            ]
        },

 

Debugging Python with VS Code

The Python extension supports debugging of a number of types of Python applications, including the following general capabilities:

  • Watch window
  • Evaluating expressions
  • Locals
  • Arguments
  • Expanding children
  • Breakpoints
  • Conditional breakpoints
  • Pausing (breaking into) running programs
  • Custom startup directory

The default launch.json (which is where VS Code stores the debugger configurations) includes a number of starter debug configurations, these are available from the configuration drop-down. The default "Python" selection provides the standard configuration. See Standard configuration and options for a description of this configuration and the debug settings.

Additional configurations are described in Debugging specific app types.

Note: If you'd like to try the new experimental debugger, see the instructions on Issue 538 (GitHub).

Standard configuration and options#

Standard configuration for launch.json:

{
    "name": "Python: Current File",
    "type": "python",
    "request": "launch",
    "stopOnEntry": true,
    "pythonPath":"${config:python.pythonPath}",
    "program": "${file}",
    "cwd": "${workspaceFolder}",
    "env": {},
    "envFile": "${workspaceFolder}/.env",
    "debugOptions": [
        "RedirectOutput"
    ]
}

Custom configurations for various settings are described in the following sections.

pythonPath#

Points to the Python interpreter to be used for debugging purposes. The standard configuration uses the interpreter identified in the python.pythonPath setting by referring to ${config:python.pythonPath}. To use a different interpreter, specify its path instead.

program#

Provides the fully qualified path to the python program's entry module. The recommended value for this is ${file}, which uses the active file in the editor. For programs with multiple files, however, you can specify the program's startup file. For example:

"program": "/Users/Me/Projects/PokemonGo-Bot/pokemongo_bot/event_handlers/__init__.py",

You can also rely on a relative path from the workspace root. For example, if the root is /Users/Me/Projects/PokemonGo-Bot then you can use the following:

"program": "${workspaceFolder}/pokemongo_bot/event_handlers/__init__.py",

args#

Specifies arguments to pass to the python program, for example:

"args": [
    "--quiet", "--norepeat"
],

stopOnEntry#

When set to true, breaks the debugger at the first line of the program being debugged. Setting to false runs the program to the first breakpoint.

console#

Specifies how program output is displayed.

Value Where output is displayed
"none" (default) No output
"integratedTerminal" VS Code debug console
"externalTerminal" Separate console window

cwd#

Specifies the current working directly for the program being debugged, defaulting to the current folder. The standard configuration uses ${workspaceFolder} to use the project folder.

debugOptions#

An array of additional options that may contain the following:

Option Description
"RedirectOutput" (default) Causes the debugger to print all output from the program into the VS Code debug output window. If this setting is omitted, all program output is not displayed in the debugger output window. This option is typically omitted when using "console": "integratedTerminal" or "console": "externalTerminal" because there's no need to duplicate the output in the debug console.
"DebugStdLib" Enabled debugging of standard library functions.
"DjangoDebugging" Activates debugging features specific to Django.
"Sudo" When used with "console": "externalTerminal", allows for debugging apps that require elevation. Using an external console is necessary to capture the password.
"Pyramid" Used when debugging a Pyramid application.

env#

Sets optional environment variables for the debugger process beyond system environment variables, which the debugger always inherits.

envFile#

Optional path to a file that contains environment variable definitions.

Debugging specific app types#

The configuration drop-down provides a variety of different options for general app types:

Configuration Description
PySpark Runs the program using PySpark instead of the standard interpreter.
Python Module Adds the setting "module": "module.name" to debug a specific module. When using this configuration, replace the value with the desired module name.
Integrated Terminal/Console Specifies "console": "integratedTerminal" and removes the RedirectOutput option.
External Terminal/Console Specifies "console": "externalTerminal" and removes the RedirectOutput option.
Django Specifies "program": "${workspaceFolder}/manage.py" and "args": ["runserver", "--noreload"], and adds "DjangoDebugging" to debugOptions. Note that automatic reloading of Django apps is not possible while debugging. To debug Django HTML templates, just add breakpoints to templates.
Flask See Flask debugging below.
Watson Specifies "program": "${workspaceFolder}/console.py" and "args": ["dev", "runserver", "--noreload=True"]
Attach (Remote Debug) See Remote debugging below.

Specific steps are also needed for remote debugging and Google App Engine. For details on debugging unit tests (including nosetest), see Unit testing.

To debug an app that requires administrator privileges, use "console": "externalTerminal" and include "Sudo" in debugOptions.

Flask debugging#

{
    "name": "Python: Flask (0.11.x or later)",
    "type": "python",
    "request": "launch",
    "stopOnEntry": false,
    "module": "flask",
    "pythonPath": "${config:python.pythonPath}",
    "cwd": "${workspaceFolder}",
    "env": {
             "FLASK_APP": "${workspaceFolder}/app.py"
    },
    "args": [
            "run",
            "--no-debugger",
            "--no-reload"
    ],
    "envFile": "${workspaceFolder}/.env",
    "debugOptions": [
             "RedirectOutput"
    ]
},

As you can see, this configuration specifies "stopOnEntry": false, "env": {"FLASK_APP": "${workspaceFolder}/app.py"} and "args": ["run", "--no-debugger","--no-reload"]. The "module": "flask" property is used instead of program.

Remote debugging#

Remote debugging allows you to step through a program locally within VS Code while it's executed on a remote computer. In this case, it's necessary to have the source code on both computers.

  1. On both development and remote computers, install ptvsd 3.0.0 (version 3.0.0 exactly - later versions are not yet supported #514).

  2. In the source code on both computers, add the following lines, replacing my_secret with the appropriate passphrase to authenticate remote debugging, and replacing address with the appropriate IP address (or localhost) and port number:

    import ptvsd
    ptvsd.enable_attach("my_secret", address = ('0.0.0.0', 3000))
    
    # Enable the line of source code below only if you want the application to wait until the debugger has attached to it
    #ptvsd.wait_for_attach()
    
  3. On the remote computer only, uncomment the last line above. You want to preserve the commented line on the development machine to ensure that source code on both machines match line for line.

  4. Start the remote program.

  5. Select the Attach (Remote Debug) configuration (below), then modify remoteRoot to point to the program's location on the remote computer, and modify host, port, and secret to match the values in the source code added above.

    {
        "name": "Attach (Remote Debug)",
        "type": "python",
        "request": "attach",
        "localRoot": "${workspaceFolder}",
        "remoteRoot": "${workspaceFolder}",
        "port": 3000,
        "secret": "my_secret",
        "host": "localhost"
    }
    

Debugging over SSH

Windows:

  1. Enable ssh port forwarding on the remote computer using sshd_config or similar.
  2. Establish a PuTTY SSH tunnel:
    1. Read Setting up an SSH tunnel with PuTTY (until "Open the session" section).
    2. On the Tunnels screen, using a local mode, source port (the port which is the entry point on the local computer) can be different from the destination port (the end point on the server).
    3. Destination address should be the localhost or 127.0.0.1 address (which is the address that the remote SSH server uses to establish the tunnel).

Linux:

  1. Run ssh -L sourceport:localhost:destinationport user@remoteaddress

Next, verify that you can see a prompt in the SSH session. Then open VS Code and configure the port to the debug port shown on the Tunnels screen.

Finally, launch the program and attach the debugger as described in the previous section.

Google App Engine debugging#

Google App Engine launches an app by itself, so launching it in the VS Code debugger isn't directly possible. What's required instead is to use ptvsd in the app and then launch Google App Engine in a mode that allows VS Code to attach its debugger.

  1. Download ptvsd and extract its files into a ptvsd folder in your working folder. (If using a different folder, modify the path in the pydev_startup.py file created in step 4).

  2. Create a tasks.json file with the following contents:

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Launch Google App Engine",
                "command": "python",
                "type": "shell",
                "args": [
                    "/usr/local/google_appengine/dev_appserver.py",
                    "--python_startup_script=${workspaceFolder}/pydev_startup.py",
                    "--automatic_restart=no",
                    "--max_module_instances=default:1",
                    "${workspaceFolder}/app.yaml"
                ]
            }
        ]
    }
    
  3. On Windows and Linux, replace the first item in args with the path to wherever Google App Engine is installed (the path shown in the source code above is for macOS).

  4. Create a file named pydev_startup.py in your project root with the following contents, modified as noted:

    import sys
    import os
    
    #Assuming that pdvsd is located in the working folder
    sys.path.append(os.getcwd())
    
    import ptvsd
    # Modify the secret and port number as desired; you're debugging locally so the values don't matter.
    # However, be sure the port is not blocked on your computer.
    ptvsd.enable_attach(secret = 'gae', address = ('0.0.0.0', 3000))
    
    #The debug server has started and you can now use VS Code to attach to the application for debugging
    print("Google App Engine has started, ready to attach the debugger")
    
  5. Create a launch.json configuring using the Attach (Remote Debug) configuration as a template. Make sure the secret and port values match what's in the source code above.

  6. Add "preLaunchTask": "python" to launch.json.

  7. From the Command Palette, run the Run Build Task command. This opens the Tasks output window where you see various messages.

  8. Once you see the message "Google App Engine has started, ready to attach the debugger", start the VS Code debugger using the remote debugging configuration.

  9. Set breakpoints where you want, then start the browser to start the app.

Troubleshooting#

Debugger not working#

There are many reasons why the debugger may not work; oftentimes the debug console reveals specific causes. Some specific reasons are described in the following table:

Cause Solution
The path to the python executable is incorrect Check the value in the pythonPath setting of settings.json.
Invalid expressions in watch window (see detailed output below) Clear all expressions from the Watch window restart the debugger.

Detailed output for invalid expressions:

Traceback (most recent call last):
  File ".../visualstudio_py_debugger.py", line 1646, in loop
    cmd()
  File ".../visualstudio_py_debugger.py", line 1918, in command_execute_code
    thread.run_on_thread(text, cur_frame, eid, frame_kind, repr_kind)
  File ".../visualstudio_py_debugger.py", line 1246, in run_on_thread
    self.schedule_work(lambda : self.run_locally(text, cur_frame, execution_id, frame_kind, repr_kind))
  File ".../visualstudio_py_debugger.py", line 1238, in schedule_work
    self.unblock()
  File ".../visualstudio_py_debugger.py", line 1234, in unblock
    self._block_lock.release()
RuntimeError: release unlocked lock

Unable to capture user input while debugging#

Capturing user input while debugging is possible only when using the "console": "externalTerminal" configuration. The integrated terminal does not support capturing user input.

Next steps#

  • Python environments - Control which Python interpreter is used for editing and debugging.

  • Unit testing - Configure unit test environments and discover, run, and debug tests.

  • Settings reference - Explore the full range of Python-related settings in VS Code.

  • General debugging - Learn about the debugging features of VS Code.

转载于:https://my.oschina.net/u/1777508/blog/1635054

你可能感兴趣的:(vscode 调试python内置库断不下来的问题)