How can I debug Python 3 code in Visual Studio Code?

后端 未结 6 588
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 04:38

I want to debug a project written in Python 3 in Visual Studio Code, but I can\'t seem to find any way of specifying interpreter or Python version in the launch.json file.

相关标签:
6条回答
  • 2020-12-15 04:59

    As of September 2016 (according to the GitHub repository documentation of the extension), you can just execute a command from within Visual Studio Code that will let you select the interpreter from an automatically generated list of known interpreters (including the one in your project's virtual environment).

    Execute:

    Python: Select Workspace Interpreter
    

    For later version, just:

    Python: Select Interpreter
    

    in the command palette (F1 for Windows, Ctrl + Shift + P for macOS).

    Then select one of the Python interpreters shown in a drop down list. And that's it. Your settings.json file will be edited automatically to point to the interpreter you selected.

    Source: Don Jayamanne's extension's documentation at GitHub

    Note: Since I couldn't comment to the accepted answer I copied some of my answer to a similar question.

    0 讨论(0)
  • 2020-12-15 05:09

    We can configure the debug in Python 3 in file settings.json:

    Menu FilePreferencesSettings (file ~/.config/Code/User/settings.json) (User Settings)

    {
      ...
      "python.pythonPath": "python3",
    }
    

    Also, verify that the launch.json file already has the following configuration:

    "configurations": [
      {
        "name": "Python: Current File (Integrated Terminal)",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal"
      },
      ...
    ]
    
    0 讨论(0)
  • 2020-12-15 05:11

    Python 3 debugging works well also. It is a little confusing as there are two different places to specify the path: settings.json and launch.json.

    I recommend using Don Jayamanne's Python Extension. After installing it, you have to configure the path to the interpreter you want to use it with.

    Python Version used for Intellisense, autocomplete, linting, formatting, etc.

    The same Python interpreter is used for intellisense, autocomplete, linting, formatting, etc. (everything other than debugging). The standard interpreter used is the first Python interpreter encountered in the current path. If a different version is to be used, this can be configured in one of two ways:

    Configure the path to the python interpreter in the User Settings file (settings.json) as follows. Ensure to specify the fully qualified name of the python executable. "python.pythonPath": "c:/python27/python.exe"

    Configure the path to the Python interpreter in the Workspace Settings file (settings.json) as follows. Ensure to specify the fully qualified name of the Python executable. "python.pythonPath": "c:/python27/python.exe" Python Version used for debugging

    Details on configuration settings for debugging can be found here Debugging. Simply provide the fully qualified path to the python executable in the "python" setting within the configuration settings in the launch.json file as follows:

    {
        "name": "Python",
        "type": "python",
        "request": "launch",
        "stopOnEntry": true,
        "program": "${file}",
        "pythonPath": "c:/python27/python.exe",
        "debugOptions": [
            "WaitOnAbnormalExit",
            "WaitOnNormalExit",
            "RedirectOutput"
        ]
    }
    
    0 讨论(0)
  • 2020-12-15 05:13

    An extra note for those using the Anaconda Python distribution by continuum analytics: you may find my experience useful.

    I'm using Don Jayamanne's Python extension and run the "Select Workspace Interpreter" command, but still found I was getting linting advice for the wrong version of Python.

    The fix that worked for me was installing the Pylint package for Anaconda:

    conda install -c anaconda pylint
    
    0 讨论(0)
  • 2020-12-15 05:19

    There is a setting trigger within the setting file:

    For Python default (which is 2.7 for now)

    "python.pythonPath": "python",
    

    For Python 3:

    "python.pythonPath": "python3",
    

    Use the following command to check the Python version:

    import sys
      print(sys.version)
    
    0 讨论(0)
  • 2020-12-15 05:19

    To debug Python projects in Visual Studio Code, use the Launch Configuration as below:

            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${cwd}/{filename}.py",
            "args" : [
                "--arg1", "arg1-value",
                "--arg2", "arg2-value",
                "--arg3", "arg3-value"
            ],
            "console": "integratedTerminal"
    
    0 讨论(0)
提交回复
热议问题