【问题标题】:Python with Visual Studio Code - Run specific file带有 Visual Studio Code 的 Python - 运行特定文件
【发布时间】:2018-03-05 14:57:35
【问题描述】:

我正在使用 Visual Studio Code 和 Python 编写一个小型应用程序。我的应用程序有这些文件:

Main.py
MyCustomClass.py

基本上,Main.py 是应用程序的入口点。另一个类只是解决一些问题的逻辑。

在开发代码时,我会通过运行它一步一步地对其进行测试。我使用 F5 运行应用程序,它正在运行我正在编辑的当前文件。它可能是文件MyCustomClass.py,它根本没有入口点,我正在浪费时间在文件之间交换。

是否可以将 Visual Studio Code 配置为在运行 (F5) 时运行特定文件 (Main.py)?无论我当前正在查看哪个文件。

【问题讨论】:

    标签: python visual-studio-code


    【解决方案1】:

    您需要编辑您的 launch.json 文件。一个快速的方法是点击the wrench icon on the run toolbar


    然后将以下内容添加到 launch.json 文件中:

        {
            "name": "Python: main.py (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/main.py",
            "console": "integratedTerminal"
        },
    

    以上是我的配置,“程序”这一行有什么神奇之处。

    【讨论】:

      【解决方案2】:

      启动配置设置中的program 设置是指将要执行的 Python 脚本。默认情况下,它设置为${file},它指的是您正在编辑的文件。只需将值设置为您要运行的文件即可。

      {
         "name": "Python",
         "type": "python",
         "request": "launch",
         "stopOnEntry": true,
         "pythonPath": "${config:python.pythonPath}",
         "program": "main.py",  // Specify the full path to your file
         "cwd": "${workspaceFolder}",
         "env": {},
         "envFile": "${workspaceFolder}/.env",
         "debugOptions": [
             "RedirectOutput"
         ]
      },
      

      我应该提到,如果您使用的是 Windows,您可以在程序路径中使用正斜杠 / 或双反斜杠 \\。单反斜杠不起作用。

      【讨论】:

      • 我应该提到,如果您使用的是 Windows,那么您可以使用正斜杠 ("/") 或双反斜杠 ("\\")。单反斜杠不起作用。
      • 是的,但将其修改为您的答案作为编辑,而不是问题。
      猜你喜欢
      • 2021-04-28
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      • 1970-01-01
      • 2021-03-28
      相关资源
      最近更新 更多