2. VS Code 开发环境配置(基于 Conda + Python)

82 阅读1分钟

VS Code 开发环境配置(基于 Conda + Python)


🪄 一、安装 VS Code

🔧 推荐扩展(Extensions)

扩展名功能
Python (Microsoft 官方)核心 Python 支持
Pylance智能补全与类型提示
Jupyter运行 .ipynb 笔记本
Code Runner一键运行脚本
GitLens版本控制可视化
Bracket Pair Colorizer 2括号高亮
Chinese (Simplified) Language Pack中文界面支持(可选)

⚙ 二、让 VS Code 识别 Conda 环境

  1. 打开 VS Code
  2. 使用快捷键:
    • Windows / Linux: Ctrl + Shift + P
    • macOS: Cmd + Shift + P
  3. 输入: Python: Select Interpreter
  4. 从列表中选择你的 Conda 环境,例如:Conda env: video-gen (Python 3.10)

💡 如果 VS Code 没检测到环境,执行以下命令:

conda activate video-gen
code .

这样会自动让 VS Code 识别该环境。


三、项目配置文件

在项目根目录创建文件夹 .vscode/,并添加以下两个配置文件。

1️⃣ settings.json

{
 "python.defaultInterpreterPath": "C:\\Users\\你的用户名\\miniconda3\\envs\\video-gen\\python.exe",
 "python.terminal.activateEnvironment": true,
 "python.languageServer": "Pylance",
 "python.analysis.autoImportCompletions": true,
 "python.formatting.provider": "black",
 "editor.formatOnSave": true,
 "python.testing.pytestEnabled": true,
 "jupyter.interactiveWindowMode": "perFile",
 "code-runner.executorMap": {
     "python": "python -u"
 }
}

2️⃣ launch.json

(调试配置)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

启动调试快捷键:

F5:运行当前文件

Ctrl + F5:运行但不调试