Python 如何使用visual studio代码调试django
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40937544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use visual studio code to debug django
提问by DEADBEEF
I'm new at djangodevelopment and come from desktop/mobile app development with Xcodeand related IDE.
我是Django开发的新手,来自使用Xcode和相关 IDE 进行桌面/移动应用程序开发。
I have to use Django and I was wondering if there was an efficient way to debug it using Visual Studio Code(or Atom).
我必须使用 Django,我想知道是否有一种使用Visual Studio Code(或Atom)调试它的有效方法。
Any help related to Django IDE would be helpful too.
任何与 Django IDE 相关的帮助也会有所帮助。
回答by Matt Bierner
For VSCode (full disclosure, I'm one of the VSCode developers) try installing the Python extensionto get started.
对于 VSCode(完全公开,我是 VSCode 开发人员之一)尝试安装Python 扩展以开始使用。
This documentation covers debugging Django. There should be a included debug configuration or you can add your own to the launch.json
file:
本文档涵盖调试 Django。应该包含调试配置,或者您可以将自己的配置添加到launch.json
文件中:
{
"name": "Django",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config.python.pythonPath}",
"program": "${workspaceRoot}/manage.py",
"args": [
"runserver",
"--no-color",
"--noreload"
],
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput",
"DjangoDebugging"
]
}
The Python extension also provide many other features that you may find useful.
Python 扩展还提供了许多您可能会觉得有用的其他功能。
Hope that helps you get started.
希望能帮助您入门。
回答by Rik Schoonbeek
VSCode has an official tutorial explaining this:
VSCode 有一个官方教程解释了这一点:
https://code.visualstudio.com/docs/python/tutorial-django
https://code.visualstudio.com/docs/python/tutorial-django
There are several steps that need to be taken, which I don't all want to write out manually, since there are quite some steps, but I'll try to summarize what needs to be done:
有几个步骤需要采取,我不想都手动写出来,因为步骤相当多,但我会尽量总结需要做的事情:
The text below is basically a partial copy of the above tutorial, I am not claiming I came up with this myself.
下面的文字基本上是上述教程的部分副本,我并不是说这是我自己想出来的。
1. Make sure to check out the prerequisites(use VS Code Python extension, install Python on local machine) link to docs
1. 确保查看先决条件(使用 VS Code Python 扩展,在本地机器上安装 Python)链接到文档
2. Use Python virtual environmentlink to docs
2.使用Python虚拟环境链接到文档
Besides using a Python virtual environment, you also need to select the Python executable inside this virtual environment as the interpreter in VS Code. This can be done like so:
除了使用 Python 虚拟环境之外,还需要选择该虚拟环境中的 Python 可执行文件作为 VS Code 中的解释器。这可以像这样完成:
In VS Code, open the Command Palette (View > Command Palette or (Ctrl+Shift+P)). Then select the Python: Select Interpreter
在 VS Code 中,打开命令面板(查看 > 命令面板或 (Ctrl+Shift+P))。然后选择 Python:选择解释器
Then you select the Python executable inside your virtual environment, which you can recognize by it's path.
然后你在你的虚拟环境中选择 Python 可执行文件,你可以通过它的路径来识别。
3. Create debugger lauch profile
3. 创建调试器 lauch 配置文件
as described here, in the documentation
upper left of the VS Code window)
VS Code 窗口的左上角)
4. Now you can start debugging
4. 现在可以开始调试了
this part of the documentation will give you an introduction on how to do that
回答by dmitry
Only experimentalconfiguration works for me.
只有实验配置对我有用。
{
"name": "Django",
"type": "pythonExperimental",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
{
"name": "Django",
"type": "pythonExperimental",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
Standard config causes Unverified breakpoint
issue.
标准配置导致Unverified breakpoint
问题。