Skip to main content

用VSCode搭建前端Python开发环境

1. 本文目标

这篇文章主要介绍如何使用VSCode来进行Python项目的开发,用VSCode来搭建一套Python跨平台开发环境,涵盖环境配置、插件安装、调试配置等内容。

2. 准备工作

官网下载VSCode并安装:https://code.visualstudio.com/

3. 安装Python

3.1. Windows

  1. 访问 Python 官网
  2. 下载最新的 Python 安装包
  3. 运行安装程序,重要:勾选 "Add Python to PATH"
  4. 选择 "Install Now" 或 "Customize installation"
  5. 完成安装

3.2. Linux(Ubuntu)

# 更新包列表
sudo apt update
sudo apt upgrade

# 安装 Python 3
sudo apt install python3

# 安装 pip
sudo apt install python3-pip

# 安装 venv(虚拟环境)
sudo apt install python3-venv

3.3. macOS

# 安装 Homebrew(如果尚未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 安装 Python
brew install python

# 或者安装特定版本
brew install python@3.11

3.4. 验证是否安装成功

# 验证安装
python3 --version
pip3 --version

4. 安装插件

VSCode通过丰富的插件生态系统来支持Python开发。以下是Python开发必备和推荐的插件:

插件名称发布者功能介绍
PythonMicrosoftPython语言支持,包含智能提示、调试、代码导航等功能
PylanceMicrosoft高性能语言服务器,提供更好的智能提示和类型检查
Python DebuggerMicrosoft专门的Python调试器
autoDocstring Docstring GeneratorNils Werner自动生成Python文档字符串
Python Test ExplorerLittle Fox Team测试资源管理器,支持unittest、pytest等
Python Type HintMatan K类型提示辅助工具
JupyterMicrosoftJupyter 是一个开源的交互式笔记本工具,允许你在网页中直接编写和运行代码、展示可视化结果,并混合编写文档说明,尤其受数据科学和机器学习领域的青睐。
Rainbow CSVmechatronerCSV文件高亮显示,执行类似SQL的查询。常用语数据科学领域。

更多编程开发通用的插件(如GitLens、TODO Highlight等),可参考《VSCode通用插件

5. 插件的用法

5.1. Python

5.1.1. 插件介绍

Python插件是VSCode中进行Python开发的核心插件,提供以下功能:

  • 代码自动补全和智能提示
  • 代码调试支持
  • 代码导航和跳转
  • 代码格式化
  • 虚拟环境管理
  • 测试框架集成

5.1.2. 插件配置

settings.json:

{
// Python解释器路径
"python.defaultInterpreterPath": "python",
// 启用自动补全
"python.autoComplete.extraPaths": [],
// 启用类型检查
"python.analysis.typeCheckingMode": "basic",
// 代码格式化工具
"python.formatting.provider": "autopep8",
// 保存时自动格式化
"editor.formatOnSave": true,
// 代码检查工具
"python.linting.enabled": true,
"python.linting.pylintEnabled": true
}

5.2. Pylance

5.2.1. 插件介绍

Pylance是Microsoft开发的高性能Python语言服务器,提供:

  • 更快的代码补全
  • 更好的类型检查
  • 代码导航
  • 自动导入

5.2.2. 插件配置

{
"python.analysis.autoImportCompletions": true,
"python.analysis.typeCheckingMode": "basic",
"python.analysis.diagnosticMode": "workspace"
}

5.3. Python调试配置

5.3.1. 创建launch.json

在项目根目录创建.vscode/launch.json文件:

{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"args": []
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": ["runserver"],
"django": true
}
]
}

5.4. 代码格式化配置

5.4.1. 安装格式化工具

pip install autopep8 black yapf

5.4.2. 配置格式化器

{
"python.formatting.provider": "black",
"python.formatting.blackArgs": [
"--line-length=88"
],
"[python]": {
"editor.defaultFormatter": "ms-python.python",
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
}
}

5.5. 代码检查配置

5.5.1. 安装代码检查工具

pip install pylint flake8 mypy

5.5.2. 配置代码检查

{
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.flake8Enabled": false,
"python.linting.mypyEnabled": false,
"python.linting.pylintArgs": [
"--disable=C0111",
"--enable=W0613"
]
}

5.6. 测试配置

5.6.1. 配置pytest

{
"python.testing.pytestEnabled": true,
"python.testing.unittestEnabled": false,
"python.testing.pytestArgs": [
"tests",
"-v"
]
}

5.7. Jupyter笔记本配置

5.7.1. 基本配置

{
"jupyter.jupyterServerType": "local",
"jupyter.askForKernelRestart": false,
"jupyter.enableNativeNotebookEditor": true
}

6. Python开发常用的快捷键

Windows/LinuxmacOS功能描述
Ctrl + Shift + PCmd + Shift + P打开命令面板
F5Fn + F5启动调试
F9Fn + F9切换断点
F12Fn + F12跳转到定义
Alt + F12Option + F12查看定义
Ctrl + /Cmd + /注释/取消注释
Shift + EnterShift + Enter在Python交互窗口中运行当前行
Ctrl + Shift + EnterCmd + Shift + Enter运行当前文件

7. 项目配置示例

7.1. 基本项目结构

my_project/
├── .vscode/
│ ├── settings.json
│ └── launch.json
├── src/
│ ├── __init__.py
│ └── main.py
├── tests/
│ └── test_main.py
├── requirements.txt
└── README.md

7.2. 推荐的settings.json配置

{
"python.defaultInterpreterPath": "./venv/bin/python",
"python.analysis.typeCheckingMode": "basic",
"python.formatting.provider": "black",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.testing.pytestEnabled": true,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
}

8. 参考文档