[英]Debug Python module using VS Code by pressing F5
My project structure is as follows:我的项目结构如下:
Project Folder
--setup.py
----Module Folder
------ __init__.py
------ __main__.py
My __main__.py
file contains the entry point to my application and the setup file is configured like this:我的
__main__.py
文件包含我的应用程序的入口点,并且安装文件配置如下:
from setuptools import setup
setup(name='my_project',
version='0.1.0',
packages=['my_project'],
entry_points={
'console_scripts': [
'my_project= my_project.__main__:main'
]})
This means I can run my code without the debugger attached using:这意味着我可以在没有附加调试器的情况下运行我的代码:
python -m my_project
I've tried debugging using VS Code by navigating to my __main__.py
file and pressing F5 to run but this doesn't work and throws an exception.我已经尝试通过导航到我的
__main__.py
文件并按 F5 运行使用 VS Code 进行调试,但这不起作用并引发异常。 How do I configure Visual Studio Code to run this module in debug mode?如何配置 Visual Studio Code 以在调试模式下运行此模块? Also how do I ensure the program also runs the module and not the file I am looking at when I press F5?
另外我如何确保程序也运行模块而不是我按 F5 时正在查看的文件?
The accepted answer didn't work for me (VSCode 1.49.0), and I got an error message reading: Invalid message: "program", "module", and "code" are mutually exclusive
.接受的答案对我不起作用(VSCode 1.49.0),我收到一条错误消息:
Invalid message: "program", "module", and "code" are mutually exclusive
。
Removing the "program": "${file}",
line solved the issue.删除
"program": "${file}",
行解决了这个问题。 I think this makes sense, since defining both a module (with an implied entrypoint) and a file is redundant.我认为这是有道理的,因为同时定义一个模块(带有一个隐含的入口点)和一个文件是多余的。
My launch.json
looks like this:我的
launch.json
看起来像这样:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python Module",
"type": "python",
"request": "launch",
"console": "integratedTerminal",
"module": "my_project",
}
]
}
After some research I've found a solution:经过一番研究,我找到了一个解决方案:
launch.json
file for this project.launch.json
文件。 This will be used to configure VS Code.launch.json
file be created, into it paste this:launch.json
文件,请将其粘贴到其中:{
"version": "0.2.0",
"configurations": [
{
"name": "Python Module",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"pythonPath": "${config:python.pythonPath}",
"module": "my_project",
"cwd": "${workspaceRoot}",
}
]
}
I found this code here: https://github.com/DonJayamanne/pythonVSCode/issues/518#issuecomment-260838308我在这里找到了这个代码: https : //github.com/DonJayamanne/pythonVSCode/issues/518#issuecomment-260838308
Just using this answer didn't work for me though and I got the error: No module named my_project
but I found this answer: https://github.com/DonJayamanne/pythonVSCode/issues/826 In it the final comment tells you add the following to the config.只是使用这个答案对我不起作用,我得到了错误:
No module named my_project
但我找到了这个答案: https : //github.com/DonJayamanne/pythonVSCode/issues/826在它最后的评论告诉你添加以下是配置。
"env": {"PYTHONPATH":"${workspaceRoot}"},
This fixes the error and now you can press F5 and your module will be debugged.这修复了错误,现在您可以按 F5,您的模块将被调试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.