How can I debug Python3 code in Visual Studio Code

2020-02-09 06:38发布

问题:

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

It works great for Python 2, so the question is, how do I make it work for Python 3?

回答1:

Python3 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"
    ] }


回答2:

Since I cannot comment to the accepted answer I'll copy some of my answer to a similar question.

As of September 2016 (according to the Github repo documentation of the extension) you can just execute a command from within vscode that will let you select the interpreter from an automatically generated list of known interpreters (including the one in your project's virtual enviroment).

Execute:

Python: Select Workspace Interpreter

Update This command has been updated to just:

Python: Select Interpreter

in the command pallet (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 will be edited automatically to point to the interpreter you selected.

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



回答3:

There is a setting trigger within the setting file:

for python default(which is 2.7 for now)

"python.pythonPath": "python",

for python3:

"python.pythonPath": "python3",

use the follow command to check the python version:

import sys
  print(sys.version)


回答4:

We can configure the debug in python3 in settings.json:

File > Preferences > Settings (~/.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"
  },
  ...
]


回答5:

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