如何在虚拟环境中的 VS Code 中运行 Python 文件?

问题描述 投票:0回答:1

在我的文件夹中

folder1
我有一个python venv,我已在终端中使用
source .venv/bin/activate
激活它。

然后,我使用

python3.12 file1.py

在终端中运行我的 python 文件

如何通过 VS Code 的

launch.json
运行它?

python visual-studio-code virtual-environment
1个回答
0
投票

安装 vscode 的 python 扩展 python 扩展

在出现Python版本的右下角点击它,它会提示你选择你想要使用的Python解释器。选择与您的虚拟环境关联的一个。 选择翻译员

现在按 f5,它会提示您选择调试器。由于您已经安装了 python 扩展,您应该会看到 python 调试器出现。选择Python调试器。 选择调试器

接下来您必须选择调试配置。您可以根据您正在构建的内容或正在使用的框架进行选择。在你的情况下,你正在运行一个 python 文件。第一个选择就可以了。 调试器配置

这将运行您的文件。然后在调试窗口的左侧面板上单击显示“创建 launch.json 文件”的链接。 创建launch.json文件

执行与之前相同的过程,选择一个调试器,然后选择一个调试器配置,然后这将根据调试器及其配置为您创建一个 launch.json 文件。

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python Debugger: Current File",
      "type": "debugpy",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal"
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.