1. 创建虚拟环境
python -m venv venv2. 选择虚拟环境解释器
- 打开 VSCode。
- 按下 Ctrl+Shift+P(macOS 是 Cmd+Shift+P)。
- 输入并选择 “Python: Select Interpreter”。
- 选择你的虚拟环境解释器,例如:
- Linux/macOS: ./venv/bin/python
- Windows: ./venv/Scripts/python.exe
3. 配置调试文件
为了确保调试时使用虚拟环境,可以修改 VSCode 的 launch.json 文件:
- 点击左侧 “运行和调试” 图标,或者按下 Ctrl+Shift+D。
- 点击顶部的 “创建 launch.json 文件”,选择 Python。
- 编辑生成的 launch.json 文件,确保指向虚拟环境解释器:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: 当前文件",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"python": "${workspaceFolder}/venv/bin/python" // Linux/macOS
// 或
// "python": "${workspaceFolder}\\venv\\Scripts\\python.exe" // Windows
}
]
}4. 手动激活虚拟环境
.\venv\Scripts\activate
source venv/bin/activate
评论 (0)