冠心病用什么药| 睡觉咬牙是什么原因| 走肾不走心什么意思| 心脏除颤是什么意思| 耐药性是什么意思| 牙痛挂什么科| 青岛是鲁什么| 喝冰水牙疼是什么原因| 仪表堂堂是什么生肖| 狗脊是什么东西| 狗喜欢吃什么食物| 老公工作劳累炖什么汤| 义诊是什么意思| 为什么会得痛风| 凯格尔运动是什么| 替班是什么意思| 晚上十点是什么时辰| 万兽之王是什么动物| 藩台是什么官| 打完升白针有什么反应| 逍遥丸适合什么人吃| 阳历三月是什么星座| 引火下行是什么意思| 涩是什么意思| 支气管炎挂什么科| 护士一般什么学历| 梅尼埃综合症是什么病| 出差什么意思| 大夫古代指什么| 女生的名字叫什么好听| 湿热泄泻是什么意思| 松树像什么| 疱疹用什么药可以根治| 微量元素挂什么科| 女生真空是什么意思| 女生两个月没来月经是什么原因| 四肢麻木是什么原因引起的| semir是什么牌子| 女性多吃什么滋补卵巢| 糖类抗原什么意思| 42年属什么生肖| 腋下淋巴结肿大挂什么科| 包皮嵌顿是什么| 四叶草是什么意思| 鲁迅字什么| 猫翘尾巴是什么意思| 经常长溃疡是什么原因引起的| 毛重是什么| 科长是什么级别| 正常白带是什么颜色| 宫颈切片检查是什么| 黄金是什么| 本科专科有什么区别| 梦见掰玉米是什么意思| tel是什么意思啊| 什么米叫粳米| 下葬下雨是什么兆头| 宫颈病变是什么意思| 貔貅长什么样| 无致病菌生长是什么意思| 佝偻病缺少什么元素| 肋骨骨折挂什么科| 肛瘘是什么情况| 泡泡尿是什么毛病| 吐司是什么| 甲抗是什么原因引起的| 手背发黄是什么原因| 红细胞阳性是什么意思| 榴莲树长什么样子| 胎盘1级什么意思| 秦始皇的真名叫什么| 女娲姓什么| 嚼槟榔为什么上瘾| 总流鼻血是什么原因| 心脏跳得快是什么原因| 栀是什么意思| 恋爱是什么| 男生吃菠萝有什么好处| 羊日冲牛是什么意思| 眼睛黄是什么病| 处暑是什么意思| 梦见新房子是什么意思| 亚临床甲亢是什么意思| 生产周期是什么意思| 卵圆孔未闭是什么病| 土耳其是什么民族| 头昏脑涨是什么原因| 掉头发去医院挂什么科| msm是什么药| 兼性厌氧是什么意思| 贴膏药发热是什么原因| 糖化血红蛋白高是什么原因| 藏头诗什么意思| 为什么会多囊| 拾人牙慧的意思是什么| 牵牛花是什么颜色的| 虫草对身体有什么好处| 了口是什么字| 女娲为什么要补天| 虎头蛇尾是什么生肖| 糖是什么意思| 梦见喝水是什么意思| 什么树林| 乳头痛什么原因| 海带和什么菜搭配好吃| 什么是柏拉图式的爱情| 什么水果是发物| 犯罪心理学属于什么专业| 蛇缠腰是什么病怎么治| 梦见买鸡蛋是什么意思周公解梦| 12颗珠子的手串什么意思| 宫颈多发纳囊什么意思| 膀胱癌有什么症状| 吃什么水果容易排便| 做爱时间短吃什么药好| 什么罩杯最大| 419是什么意思| 菜鸟裹裹是什么快递| 奥美拉唑什么时候吃| 女孩什么时辰出生最好| 一九六八年属什么生肖| 女性尿路感染是什么原因造成的| 268数字代表什么意思| 什么网站| 牛排和什么一起炖好吃| 乳头痛是什么原因| 10.19什么星座| 书到用时方恨少下一句是什么| 向内求什么意思| bmi是什么| 超负荷是什么意思| 花苞裤不适合什么人穿| 一个黑一个今念什么| 三月十七是什么星座| 萧墙是什么意思| 山楂和什么相克| exchange是什么意思| 芈怎么读什么意思| 右肾盂分离是什么意思| 批发零售属于什么行业| 每次上大便都出血是什么原因| 经信局是做什么的| a型血和ab型血生的孩子是什么血型| 福建人喜欢吃什么口味| 黄金果是什么水果| 上海为什么叫魔都| 原籍是什么意思| 天降甘霖什么意思| 处女膜是什么颜色| 刚生完宝宝的产妇吃什么好| 鸡炖什么好吃又有营养| 西安古代叫什么名字| 武松打的是什么虎| 四川酸菜是什么菜| 离婚都需要什么手续和证件| 白化病有什么危害吗| 肛周湿疹用什么药膏| 红豆有什么功效和作用| 化疗恶心吃什么可以缓解| 肠上皮化生是什么意思| 泰安有什么大学| 13颗珠子的手串什么意思| 什么水果含铁| 发烧是什么感觉| 什么的滋味| 柠檬什么季节成熟| 糖筛和糖耐有什么区别| 手脚出汗多是什么原因| 皮肤过敏吃什么药| 丙氨酸氨基转移酶高吃什么药| 什么什么不宁| 手腕三条纹代表什么| 炒菜什么时候放盐最合适| 包干是什么意思| 怨气是什么意思| 突然抽搐失去意识是什么原因| 猫咪感冒吃什么药| 风湿吃什么药好| 3月5日是什么星座的| 脾大是什么原因| 12月7号什么星座| 脚丫痒是什么原因| 中国国花是什么花| 雌二醇高说明什么| 窦性心律是什么| 艾滋通过什么途径传播| 屎黄色是什么颜色| 卯是什么生肖| 浪子是什么意思| 飞机不能带什么| 肺气肿用什么药效果好| 人棍是什么意思| 脚气用什么药| pbs是什么| 尿酸高是什么原因导致的| 梦见红棺材是什么征兆| 经期提前是什么原因| 叶黄素对眼睛有什么功效| 舌头上有红点点是什么原因| 多囊为什么要跳绳而不是跑步| h是什么牌子的衣服| 冬瓜什么季节吃最好| 中午吃什么减肥| 玉树临风什么意思| 喊麦是什么意思| 难受是什么意思| 肾功能四项检查什么| choice是什么意思| 掌心有痣代表什么| 喝最烈的酒下一句是什么| 芳华什么意思| 头昏脑涨是什么原因| 行政管理是做什么的| 平均红细胞体积偏高是什么原因| 泰能是什么药| 子宫轻度下垂有什么办法恢复| 鲁迅为什么弃医从文| 望闻问切是什么意思| 不是经期有少量出血是什么原因| 6月12日是什么星座| 指鼻试验阳性代表什么| 膝盖疼应该挂什么科| 爸爸的爸爸叫什么| 机不可失的下一句是什么| 自恋是什么意思| 眉毛淡的男人代表什么| 什么是纳囊| 2024年属什么生肖| 嘴炮是什么意思| 七四年属什么生肖| 手脚出汗什么原因| 维生素c有什么好处| 手筋鼓起来是什么原因| 油嘴滑舌是什么意思| 托班是什么意思| 秋高气爽是什么意思| 舌苔厚白中间有裂纹吃什么药| 头不舒服去医院挂什么科| 李登辉是什么人| 重丧是什么意思| 舌头有点麻是什么病的前兆| 景色什么| 胃肠造影主要检查什么| 屋漏偏逢连夜雨是什么意思| 紫玉是什么玉| 快递属于什么行业| 类似蜈蚣的虫子叫什么| 为什么会长痤疮| 拿铁和美式有什么区别| 人为什么会哭| 乌鱼蛋是什么| 桃子是什么颜色| 验孕棒什么时候测比较准| 8月6日是什么星座| 手指经常抽筋是什么原因| 甲状腺功能亢进吃什么药| 做梦梦到踩到屎是什么意思| 头晕吃什么好| 金今念什么| 南京都有什么大学| 银鱼是什么鱼| 妃子笑是什么茶| 镜花水月是什么意思| 化纤是什么面料| 百度

Try to extend agent mode in VS Code!

用车夜间车行道撞上行人 责任认定会怎样?求

百度 1月,中央全面深化改革领导小组第二次会议审议通过了《关于建立城乡居民基本养老保险待遇确定和基础养老金正常调整机制的指导意见》。

The Python extension supports debugging through the Python Debugger extension for several types of Python applications. For a short walkthrough of basic debugging, see Tutorial - Configure and run the debugger. Also see the Flask tutorial. Both tutorials demonstrate core skills like setting breakpoints and stepping through code.

For general debugging features such as inspecting variables, setting breakpoints, and other activities that aren't language-dependent, review VS Code debugging.

This article mainly addresses Python-specific debugging configurations, including the necessary steps for specific app types and remote debugging.

Python Debugger Extension

The Python Debugger extension is automatically installed along with the Python extension for VS Code. It offers debugging features with debugpy for several types of Python applications, including scripts, web apps, remote processes and more.

To verify it's installed, open the Extensions view (??X (Windows, Linux Ctrl+Shift+X)) and search for @installed python debugger. You should see the Python Debugger extension listed in the results.

Python Debugger extension shown in installed extensions view in VS Code.

You can refer to the extension's README page for information on supported Python versions.

Initialize configurations

A configuration drives VS Code's behavior during a debugging session. Configurations are defined in a launch.json file that's stored in a .vscode folder in your workspace.

Note: To change debugging configuration, your code must be stored in a folder.

To initialize debug configurations, first select the Run view in the sidebar:

Run icon

If you don't yet have any configurations defined, you'll see a button to Run and Debug and a link to create a configuration (launch.json) file:

Debug toolbar settings command

To generate a launch.json file with Python configurations, do the following steps:

  1. Select the create a launch.json file link (outlined in the image above) or use the Run > Open configurations menu command.

  2. Select Python Debugger from the debugger options list.

  3. A configuration menu will open from the Command Palette allowing you to choose the type of debug configuration you want to use for our Python project file. If you want to debug a single Python script, select Python File in the Select a debug configuration menu that appears.

    List of Python debugger configuration options

    Note: Starting a debugging session through the Debug Panel, F5, or Run > Start Debugging when no configuration exists will also bring up the debug configuration menu, but will not create a launch.json file.

  4. The Python Debugger extension then creates and opens a launch.json file that contains a pre-defined configuration based on what you previously selected, in this case, Python File. You can modify configurations (to add arguments, for example), and also add custom configurations.

    Configuration json

The details of configuration properties are covered later in this article under Standard configuration and options. Other configurations are also described in this article under Debugging specific app types.

Additional configurations

By default, VS Code shows only the most common configurations provided by the Python Debugger extension. You can select other configurations to include in launch.json by using the Add Configuration command shown in the list and the launch.json editor. When you use the command, VS Code prompts you with a list of all available configurations (be sure to select the Python option):

Adding a new Python debugging configuration

Selecting the Attach using Process ID one yields the following result: Added a configuration

See Debugging specific app types for details on all of these configurations.

During debugging, the Status Bar shows the current configuration and the current debugging interpreter. Selecting the configuration brings up a list from which you can choose a different configuration:

Debugging Status Bar

By default, the debugger uses the same interpreter selected for your workspace, just like other features of Python extension for VS Code. To use a different interpreter for debugging specifically, set the value for python in launch.json for the applicable debugger configuration. Alternately, use the Python interpreter indicator on the Status Bar to select a different one.

Basic debugging

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Python Debugger: Debug Python File.

Debug button on the top-right of the editor

If you're looking to debug a web application using Flask, Django or FastAPI, the Python Debugger extension provides dynamically created debug configurations based on your project structure under the Show all automatic debug configurations option, through the Run and Debug view.

Show all automatic debug configurations option on the run view

But if you're looking to debug other kinds of applications, you can start the debugger through the Run view by clicking on the Run and Debug button.

Run the debugger

When no configuration has been set, you'll be given a list of debugging options. Here, you can select the appropriate option to quickly debug your code.

Two common options are to use the Python File configuration to run the currently open Python file or to use the Attach using Process ID configuration to attach the debugger to a process that is already running.

For information about creating and using debugging configurations, see the Initialize configurations and Additional configurations sections. Once a configuration is added, it can be selected from the dropdown list and started using the Start Debugging button (F5).

Start debugging button in the Run and Debug view

Command line debugging

The debugger can also be run from the command line, if debugpy is installed in your Python environment.

Install debugpy

You can install debugpy using python -m pip install --upgrade debugpy into your Python environment.

Tip: While using a virtual environment is not required, it is a recommended best practice. You can create a virtual environment in VS Code by opening the Command Palette (??P (Windows, Linux Ctrl+Shift+P)) and running the Python: Create Virtual Environment command ().

Command line syntax

The debugger command line syntax is as follows:

python -m debugpy
    --listen | --connect
    [<host>:]<port>
    [--wait-for-client]
    [--configure-<name> <value>]...
    [--log-to <path>] [--log-to-stderr]
    <filename> | -m <module> | -c <code> | --pid <pid>
    [<arg>]...

Example

From the command line, you could start the debugger using a specified port (5678) and script using the following syntax. This example assumes the script is long-running and omits the --wait-for-client flag, meaning that the script will not wait for the client to attach.

python -m debugpy --listen 5678 ./myscript.py

You would then use the following configuration to attach from the VS Code Python Debugger extension.

{
  "name": "Python Debugger: Attach",
  "type": "debugpy",
  "request": "attach",
  "connect": {
    "host": "localhost",
    "port": 5678
  }
}

Note: Specifying host is optional for listen, by default 127.0.0.1 is used.

If you wanted to debug remote code or code running in a docker container, on the remote machine or container, you would need to modify the previous CLI command to specify a host.

python -m debugpy --listen 0.0.0.0:5678 ./myscript.py

The associated configuration file would then look as follows.

{
  "name": "Attach",
  "type": "debugpy",
  "request": "attach",
  "connect": {
    "host": "remote-machine-name", // replace this with remote machine name
    "port": 5678
  }
}

Note: Be aware that when you specify a host value other than 127.0.0.1 or localhost you are opening a port to allow access from any machine, which carries security risks. You should make sure that you're taking appropriate security precautions, such as using SSH tunnels, when doing remote debugging.

Command line options

Flag Options Description
--listen or --connect [<host>:]<port> Required. Specifies the host address and port for the debug adapter server to wait for incoming connections (--listen) or to connect with a client that is waiting for an incoming connection (--connect). This is the same address that is used in the VS Code debug configuration. By default, the host address is localhost (127.0.0.1).
--wait-for-client none Optional. Specifies that the code should not run until there's a connection from the debug server. This setting allows you to debug from the first line of your code.
--log-to <path> Optional. Specifies a path to an existing directory for saving logs.
--log-to-stderr none Optional. Enables debugpy to write logs directly to stderr.
--pid <pid> Optional. Specifies a process that is already running to inject the debug server into.
--configure-<name> <value> Optional. Sets a debug property that must be known to the debug server before the client connects. Such properties can be used directly in launch configuration, but must be set in this manner for attach configurations. For example, if you don't want the debug server to automatically inject itself into subprocesses created by the process you're attaching to, use --configure-subProcess false.

Note: [<arg>] can be used to pass command-line arguments along to the app being launched.

Debugging by attaching over a network connection

Local script debugging

There may be instances where you need to debug a Python script that's invoked locally by another process. For example, you may be debugging a web server that runs different Python scripts for specific processing jobs. In such cases, you need to attach the VS Code debugger to the script once it's been launched:

  1. Run VS Code, open the folder or workspace containing the script, and create a launch.json for that workspace if one doesn't exist already.

  2. In the script code, add the following and save the file:

    import debugpy
    
    # 5678 is the default attach port in the VS Code debug configurations. Unless a host and port are specified, host defaults to 127.0.0.1
    debugpy.listen(5678)
    print("Waiting for debugger attach")
    debugpy.wait_for_client()
    debugpy.breakpoint()
    print('break on this line')
    
  3. Open a terminal using Terminal: Create New Terminal, which activates the script's selected environment.

  4. In the terminal, install the debugpy package.

  5. In the terminal, start Python with the script, for example, python3 myscript.py. You should see the "Waiting for debugger attach" message that's included in the code, and the script halts at the debugpy.wait_for_client() call.

  6. Switch to the Run and Debug view (??D (Windows, Linux Ctrl+Shift+D)), select the appropriate configuration from the debugger dropdown list, and start the debugger.

  7. The debugger should stop on the debugpy.breakpoint() call, from which point you can use the debugger normally. You also have the option of setting other breakpoints in the script code using the UI instead of using debugpy.breakpoint().

Remote script debugging with SSH

Remote debugging allows you to step through a program locally within VS Code while it runs on a remote computer. It is not necessary to install VS Code on the remote computer. For added security, you may want or need to use a secure connection, such as SSH, to the remote computer when debugging.

Note: On Windows computers, you may need to install Windows 10 OpenSSH to have the ssh command.

The following steps outline the general process to set up an SSH tunnel. An SSH tunnel allows you to work on your local machine as if you were working directly on the remote in a more secure manner than if a port was opened for public access.

On the remote computer:

  1. Enable port forwarding by opening the sshd_config config file (found under /etc/ssh/ on Linux and under %programfiles(x86)%/openssh/etc on Windows) and adding or modifying the following setting:

    AllowTcpForwarding yes
    

    Note: The default for AllowTcpForwarding is yes, so you might not need to make a change.

  2. If you had to add or modify AllowTcpForwarding, restart the SSH server. On Linux/macOS, run sudo service ssh restart; on Windows, run services.msc, select OpenSSH or sshd in the list of services, and select Restart.

On the local computer:

  1. Create an SSH tunnel by running ssh -2 -L sourceport:localhost:destinationport -i identityfile user@remoteaddress, using a selected port for destinationport and the appropriate username and the remote computer's IP address in user@remoteaddress. For example, to use port 5678 on IP address 1.2.3.4, the command would be ssh -2 -L 5678:localhost:5678 -i identityfile user@1.2.3.4. You can specify the path to an identity file, using the -i flag.

  2. Verify that you can see a prompt in the SSH session.

  3. In your VS Code workspace, create a configuration for remote debugging in your launch.json file, setting the port to match the port used in the ssh command and the host to localhost. You use localhost here because you've set up the SSH tunnel.

    {
      "name": "Python Debugger: Attach",
      "type": "debugpy",
      "request": "attach",
      "port": 5678,
      "host": "localhost",
      "pathMappings": [
        {
          "localRoot": "${workspaceFolder}", // Maps C:\Users\user1\project1
          "remoteRoot": "." // To current working directory ~/project1
        }
      ]
    }
    

Starting debugging

Now that an SSH tunnel has been set up to the remote computer, you can begin your debugging.

  1. Both computers: make sure that identical source code is available.

  2. Both computers: install debugpy.

  3. Remote computer: there are two ways to specify how to attach to the remote process.

    1. In the source code, add the following lines, replacing address with the remote computer's IP address and port number (IP address 1.2.3.4 is shown here for illustration only).

      import debugpy
      
      # Allow other computers to attach to debugpy at this IP address and port.
      debugpy.listen(('1.2.3.4', 5678))
      
      # Pause the program until a remote debugger is attached
      debugpy.wait_for_client()
      

      The IP address used in listen should be the remote computer's private IP address. You can then launch the program normally, causing it to pause until the debugger attaches.

    2. Launch the remote process through debugpy, for example:

      python3 -m debugpy --listen 1.2.3.4:5678 --wait-for-client -m myproject
      

      This starts the package myproject using python3, with the remote computer's private IP address of 1.2.3.4 and listening on port 5678 (you can also start the remote Python process by specifying a file path instead of using -m, such as ./hello.py).

  4. Local computer: Only if you modified the source code on the remote computer as outlined above, then in the source code, add a commented-out copy of the same code added on the remote computer. Adding these lines makes sure that the source code on both computers matches line by line.

    #import debugpy
    
    # Allow other computers to attach to debugpy at this IP address and port.
    #debugpy.listen(('1.2.3.4', 5678))
    
    # Pause the program until a remote debugger is attached
    #debugpy.wait_for_client()
    
  5. Local computer: switch to the Run and Debug view (??D (Windows, Linux Ctrl+Shift+D)) in VS Code, select the Python Debugger: Attach configuration

  6. Local computer: set a breakpoint in the code where you want to start debugging.

  7. Local computer: start the VS Code debugger using the modified Python Debugger: Attach configuration and the Start Debugging button. VS Code should stop on your locally set breakpoints, allowing you to step through the code, examine variables, and perform all other debugging actions. Expressions that you enter in the Debug Console are run on the remote computer as well.

    Text output to stdout, as from print statements, appears on both computers. Other outputs, such as graphical plots from a package like matplotlib, however, appear only on the remote computer.

  8. During remote debugging, the debugging toolbar appears as below:

    Debugging toolbar during remote debugging

    On this toolbar, the disconnect button (?F5 (Windows, Linux Shift+F5)) stops the debugger and allows the remote program to run to completion. The restart button (??F5 (Windows, Linux Ctrl+Shift+F5)) restarts the debugger on the local computer but does not restart the remote program. Use the restart button only when you've already restarted the remote program and need to reattach the debugger.

Set configuration options

When you first create launch.json, there are two standard configurations that run the active file in the editor in either the integrated terminal (inside VS Code) or the external terminal (outside of VS Code):

{
  "configurations": [
    {
      "name": "Python Debugger: Current File (Integrated Terminal)",
      "type": "debugpy",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal"
    },
    {
      "name": "Python Debugger: Current File (External Terminal)",
      "type": "debugpy",
      "request": "launch",
      "program": "${file}",
      "console": "externalTerminal"
    }
  ]
}

The specific settings are described in the following sections. You can also add other settings, such as args, that aren't included in the standard configurations.

Tip: It's often helpful in a project to create a configuration that runs a specific startup file. For example, if you want to always launch startup.py with the arguments --port 1593 when you start the debugger, create a configuration entry as follows:

 {
     "name": "Python Debugger: startup.py",
     "type": "debugpy",
     "request": "launch",
     "program": "${workspaceFolder}/startup.py",
     "args" : ["--port", "1593"]
 },

name

Provides the name for the debug configuration that appears in the VS Code dropdown list.

type

Identifies the type of debugger to use; leave this set to debugpy for debugging Python code.

request

Specifies the mode in which to start debugging:

  • launch: start the debugger on the file specified in program
  • attach: attach the debugger to an already running process. See Remote debugging for an example.

program

Provides the fully qualified path to the python program's entry module (startup file). The value ${file}, often used in default configurations, uses the currently active file in the editor. By specifying a specific startup file, you can always be sure of launching your program with the same entry point regardless of which files are open. For example:

"program": "/Users/Me/Projects/MyProject/src/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/MyProject then you can use the following example:

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

module

Provides the ability to specify the name of a module to be debugged, similarly to the -m argument when run at the command line. For more information, see Python.org

python

The full path that points to the Python interpreter to be used for debugging.

If not specified, this setting defaults to the interpreter selected for your workspace, which is equivalent to using the value ${command:python.interpreterPath}. To use a different interpreter, specify its path instead in the python property of a debug configuration.

Alternately, you can use a custom environment variable that's defined on each platform to contain the full path to the Python interpreter to use, so that no other folder paths are needed.

If you need to pass arguments to the Python interpreter, you can use the pythonArgs property.

pythonArgs

Specifies arguments to pass to the Python interpreter using the syntax "pythonArgs": ["<arg 1>", "<arg 2>",...].

args

Specifies arguments to pass to the Python program. Each element of the argument string that's separated by a space should be contained within quotes, for example:

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

If you want to provide different arguments per debug run, you can set args to "${command:pickArgs}". This will prompt you to enter arguments each time you start a debug session.

Note: There is a difference in how "${command:pickArgs}" and ["${command:pickArgs}"] are parsed, with specific notice to the usage of []. As an array, all arguments are passed as a single string, without brackets each argument is passed as its own string.

stopOnEntry

When set to true, breaks the debugger at the first line of the program being debugged. If omitted (the default) or set to false, the debugger runs the program to the first breakpoint.

console

Specifies how program output is displayed as long as the defaults for redirectOutput aren't modified.

Value Where output is displayed
"internalConsole" VS Code debug console. If redirectOutput is set to False, no output is displayed.
"integratedTerminal" (default) VS Code Integrated Terminal. If redirectOutput is set to True, output is also displayed in the debug console.
"externalTerminal" Separate console window. If redirectOutput is set to True, output is also displayed in the debug console.

purpose

There is more than one way to configure the Run button, using the purpose option. Setting the option to debug-test, defines that the configuration should be used when debugging tests in VS Code. However, setting the option to debug-in-terminal, defines that the configuration should only be used when accessing the Run Python File button on the top-right of the editor (regardless of whether the Run Python File or Debug Python File options the button provides is used). Note: The purpose option can't be used to start the debugger through F5 or Run > Start Debugging.

autoReload

Allows for the automatic reload of the debugger when changes are made to code after the debugger execution has hit a breakpoint. To enable this feature set {"enable": true} as shown in the following code.

{
  "name": "Python Debugger: Current File",
  "type": "debugpy",
  "request": "launch",
  "program": "${file}",
  "console": "integratedTerminal",
  "autoReload": {
    "enable": true
  }
}

Note: When the debugger performs a reload, code that runs on import might be executed again. To avoid this situation, try to only use imports, constants, and definitions in your module, placing all code into functions. Alternatively, you can also use if __name__=="__main__" checks.

subProcess

Specifies whether to enable subprocess debugging. Defaults to false, set to true to enable. For more information, see multi-target debugging.

cwd

Specifies the current working directory for the debugger, which is the base folder for any relative paths used in code. If omitted, defaults to ${workspaceFolder} (the folder open in VS Code).

As an example, say ${workspaceFolder} contains a py_code folder containing app.py, and a data folder containing salaries.csv. If you start the debugger on py_code/app.py, then the relative paths to the data file vary depending on the value of cwd:

cwd Relative path to data file
Omitted or ${workspaceFolder} data/salaries.csv
${workspaceFolder}/py_code ../data/salaries.csv
${workspaceFolder}/data salaries.csv

redirectOutput

When set to true (the default for internalConsole), causes the debugger to print all output from the program into the VS Code debug output window. If set to false (the default for integratedTerminal and externalTerminal), program output is not displayed in the debugger output window.

This option is typically disabled when using "console": "integratedTerminal" or "console": "externalTerminal" because there's no need to duplicate the output in the debug console.

justMyCode

When omitted or set to true (the default), restricts debugging to user-written code only. Set to false to also enable debugging of standard library functions.

django

When set to true, activates debugging features specific to the Django web framework.

sudo

When set to true and used with "console": "externalTerminal", allows for debugging apps that require elevation. Using an external console is necessary to capture the password.

pyramid

When set to true, ensures that a Pyramid app is launched with the necessary pserve command.

env

Sets optional environment variables for the debugger process beyond system environment variables, which the debugger always inherits. The values for these variables must be entered as strings.

envFile

Optional path to a file that contains environment variable definitions. See Configuring Python environments - environment variable definitions file.

gevent

If set to true, enables debugging of gevent monkey-patched code.

jinja

When set to true, activates debugging features specific to the Jinja templating framework.

Breakpoints and logpoints

The Python Debugger extension supports breakpoints and logpoints for debugging code. For a short walkthrough of basic debugging and using breakpoints, see Tutorial - Configure and run the debugger.

Conditional breakpoints

Breakpoints can also be set to trigger based on expressions, hit counts, or a combination of both. The Python Debugger extension supports hit counts that are integers, in addition to integers preceded by the ==, >, >=, <, <=, and % operators. For example, you could set a breakpoint to trigger after five occurrences by setting a hit count of >5 For more information, see conditional breakpoints in the main VS Code debugging article.

Invoking a breakpoint in code

In your Python code, you can call debugpy.breakpoint() at any point where you want to pause the debugger during a debugging session.

Breakpoint validation

The Python Debugger extension automatically detects breakpoints that are set on non-executable lines, such as pass statements or the middle of a multiline statement. In such cases, running the debugger moves the breakpoint to the nearest valid line to ensure that code execution stops at that point.

Debugging specific app types

The configuration dropdown provides various different options for general app types:

Configuration Description
Attach See Remote debugging in the previous section.
Django Specifies "program": "${workspaceFolder}/manage.py", "args": ["runserver"]. Also adds "django": true to enable debugging of Django HTML templates.
Flask See Flask debugging below.
Gevent Adds "gevent": true to the standard integrated terminal configuration.
Pyramid Removes program, adds "args": ["${workspaceFolder}/development.ini"], adds "jinja": true for enabling template debugging, and adds "pyramid": true to ensure that the program is launched with the necessary pserve command.

Specific steps are also needed for remote debugging and Google App Engine. For details on debugging tests, see Testing.

To debug an app that requires administrator privileges, use "console": "externalTerminal" and "sudo": "True".

Flask debugging

{
    "name": "Python Debugger: Flask",
    "type": "debugpy",
    "request": "launch",
    "module": "flask",
    "env": {
        "FLASK_APP": "app.py"
    },
    "args": [
        "run",
        "--no-debugger"
    ],
    "jinja": true
},

As you can see, this configuration specifies "env": {"FLASK_APP": "app.py"} and "args": ["run", "--no-debugger"]. The "module": "flask" property is used instead of program. (You may see "FLASK_APP": "${workspaceFolder}/app.py" in the env property, in which case modify the configuration to refer to only the filename. Otherwise, you may see "Cannot import module C" errors where C is a drive letter.)

The "jinja": true setting also enables debugging for Flask's default Jinja templating engine.

If you want to run Flask's development server in development mode, use the following configuration:

{
    "name": "Python Debugger: Flask (development mode)",
    "type": "debugpy",
    "request": "launch",
    "module": "flask",
    "env": {
        "FLASK_APP": "app.py",
        "FLASK_ENV": "development"
    },
    "args": [
        "run"
    ],
    "jinja": true
},

Troubleshooting

There are many reasons why the debugger may not work. Sometimes the debug console reveals specific causes, but the main reasons are as follows:

  • Make sure the Python Debugger extension is installed and enabled in VS Code by opening the Extensions view (??X (Windows, Linux Ctrl+Shift+X)) and searching for @installed python debugger.

  • The path to the python executable is incorrect: check the path of your selected interpreter by running the Python: Select Interpreter command and looking at the current value:

    Troubleshooting wrong Python interpreter when debugging

  • You have "type" set to the deprecated value "python" in your launch.json file: replace "python" with "debugpy" instead to work with the Python Debugger extension.

  • There are invalid expressions in the watch window: clear all expressions from the Watch window and restart the debugger.

  • If you're working with a multi-threaded app that uses native thread APIs (such as the Win32 CreateThread function rather than the Python threading APIs), it's presently necessary to include the following source code at the top of whichever file you want to debug:

    import debugpy
    debugpy.debug_this_thread()
    
  • If you are working with a Linux system, you may receive a "timed out" error message when trying to apply a debugger to any running process. To prevent this, you can temporarily run the following command:

    echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
    

Next steps

  • Python environments - Control which Python interpreter is used for editing and debugging.
  • Testing - Configure 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.
息肉和痔疮有什么区别 喝三七粉有什么好处 跳蚤怕什么 荪是什么意思 肠胃炎引起的发烧吃什么药
SEX是什么 腰疼吃什么 丁香茶有什么作用和功效 是对什么 口干舌燥吃什么中成药
孕妇适合吃什么鱼 前列腺增生有什么症状 公分的单位是什么 walls是什么意思 中医为什么下午不把脉
尿酸高能吃什么鱼 肝炎有什么症状 懒散是什么意思 右脸颊长痘是什么原因 发烧拉稀是什么原因
211是什么意思beikeqingting.com 包涵是什么意思hcv8jop1ns4r.cn 扑尔敏的学名叫什么hcv8jop1ns6r.cn 为什么会长粉刺hcv8jop8ns1r.cn 肆意什么意思hcv9jop8ns3r.cn
虾腹部的黑线是什么hcv8jop5ns9r.cn 苋菜不能和什么一起吃zhongyiyatai.com 上午十点是什么时辰hcv9jop2ns7r.cn 哈尔滨市长什么级别hcv9jop5ns4r.cn 面包糠是什么做的hcv9jop5ns5r.cn
甘油三酯高是什么原因hcv7jop6ns8r.cn 男生来大姨夫是什么意思hcv7jop9ns5r.cn 氨甲环酸又叫什么名hcv8jop9ns5r.cn 痱子吃什么药ff14chat.com 徒木立信是什么意思96micro.com
异性朋友是什么意思hcv8jop7ns3r.cn 农合是什么bjhyzcsm.com 肺栓塞的主要症状是什么hcv9jop0ns4r.cn 车机是什么hcv8jop8ns4r.cn 淋巴滤泡增生是什么意思严重吗qingzhougame.com
百度