如何在 Visual Studio Code 中将 python 作为主模块运行 [英] How to run python in Visual Studio Code as a main module

查看:30
本文介绍了如何在 Visual Studio Code 中将 python 作为主模块运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Visual Studio Code 中将 python 作为主模块运行?

How to run python in Visual Studio Code as a main module?

从命令行我会使用 -m 开关,比如

From the command line I would use the -m switch, like

python -m program.py

我需要这个来使相对导入工作.

I need this to make relative imports work.

有什么可以添加到 launch.json 文件中的吗?

Is there something I could add to the launch.json file?

如果这是不可能的,我可能需要对 runpy 查看 python 文档,但如果 vscode 可以做到这一点就好了.

If this isn't possible, I maybe need to do something with runpy see python docs, but it would be nice if vscode can do this.

目前,我使用一个额外的 run.py 文件作为解决方法,将其放在要运行的包之外.然后配置 vscode 来运行该文件:

For the moment I use, as a workaround, an extra run.py file which I place outside the package I want to run. Then configure vscode to run that file:

"program": "${workspaceRoot}/../run.py"

run.py 我导入包并调用它的入口函数.

From run.py I import the package and call its entry-point function.

推荐答案

可以在此处找到调试模块的文档:https://code.visualstudio.com/docs/python/debugging#_debugging-specific-app-types

The documentation for debugging a module can be found here: https://code.visualstudio.com/docs/python/debugging#_debugging-specific-app-types

您需要做的就是:

  • 在 VS Code 中选择 Python: Module 调试配置
  • 编辑 launch.json 并找到 Python: Module 配置部分,并将设置 module 的值替换为模块名称,例如程序
  • Select the Python: Module debug cofiguration in VS Code
  • Edit the launch.json and locate the Python: Module config section and replace the value for the setting module with the module name, e.g. program

这篇关于如何在 Visual Studio Code 中将 python 作为主模块运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆