Visual Studio Code - Python 中的输入函数

新手上路,请多包涵

我正在尝试使用 Visual Studio Code 来学习 Python。

我正在编写一段起始代码来获取用户的输入,例如:

 S = input("What's your name? ")

当我尝试运行它时(Mac: Cmd + Shift + B )我看到任务正在运行但没有输出。我已经为输出和参数配置了 tasks.json 文件。

 print("Hello, World!")
S = input("What's your name? ")

我需要在 Visual Studio Code 中配置一些环境变量吗?

原文由 bhanu 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 346
1 个回答

任务旨在构建您的应用程序。由于 Python 是解释型的,因此您根本不需要使用 tasks.json 来运行/调试您的 Python 代码。请改用 launch.json。我正在使用 Don Jayamanne 的 Python 扩展进行调试,并按如下方式配置了 launch.json:

  1. 打开命令面板 ( Ctrl + Shift + P ) 并编写命令:

无需调试即可启动

  1. 然后选择您的环境 -> 单击 Python。这应该在当前目录的 .vscode 目录中创建一个 launch.json 文件。

  2. 粘贴以下配置json

    {
   "version": "0.2.0",
   "configurations": [
       {
           "name": "Python",
           "type": "python",
           "request": "launch",
           "stopOnEntry": true,
           "pythonPath": "${config.python.pythonPath}",
           "program": "${file}",
           "debugOptions": [
               "WaitOnAbnormalExit",
               "WaitOnNormalExit",
               "RedirectOutput"
           ],
           "console": "integratedTerminal"
       }
   ]}

  1. 保存文件,在编辑器中打开您的 Python 脚本,然后再次“开始而不调试”。这应该启动一个集成终端,您可以在其中输入并查看输出。

原文由 mrsauravsahu 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题