VS Code(Visual Studio Code)是一款轻量级但功能强大的代码编辑器,支持多种编程语言,其中 Python 是最受欢迎的之一。下面介绍如何在 Windows / macOS / Linux 中配置 Python 开发环境。
1. 安装 VS Code
如果你还没有安装 VS Code,可以从 官方页面 下载并安装适合你操作系统的版本。
安装完成后,打开 VS Code。
2. 安装 Python 并配置环境
(1)安装 Python
如果尚未安装 Python,可以从 Python 官网 下载并安装最新版。
Windows 用户注意:
安装时 务必勾选 “Add Python to PATH”,否则 VS Code 可能无法找到 Python 解释器。
安装完成后,在终端(Command Prompt / PowerShell / macOS Terminal)运行以下命令检查是否安装成功:
python --version
或(某些系统需要用 python3 代替 python)
python3 --version
如果返回 Python 3.x.x,则安装成功。
(2)在 VS Code 中安装 Python 扩展
打开 VS Code,按下 Ctrl + Shift + X(Windows/Linux)或 Cmd + Shift + X(macOS)打开扩展市场,搜索 “Python” 并安装由 Microsoft 官方维护的 Python 扩展。
或者可以访问:
🔗 Python 扩展市场
安装完成后,重启 VS Code。
3. 配置 Python 解释器
VS Code 需要知道你的 Python 解释器路径,否则可能无法运行 Python 代码。
(1)选择 Python 解释器
- 打开 VS Code,按下
Ctrl + Shift + P(macOSCmd + Shift + P)调出命令面板。 - 输入
Python: Select Interpreter并回车。 - 选择 Python 版本(一般选择
Python 3.x.x,路径通常是C:\Users\你的用户名\AppData\Local\Programs\Python\Python3x\python.exe或/usr/bin/python3)。 - 如果没有看到 Python 解释器,可以点击
Enter interpreter path,手动输入 Python 解释器路径。
如果不确定 Python 解释器路径,可以在终端运行:
which python3 # macOS / Linux
where python # Windows
4. 创建 Python 项目
(1)创建 Python 文件
- 在 VS Code 中打开一个文件夹(点击
File -> Open Folder)。 - 创建
hello.py文件,输入以下代码:
print("Hello, Python in VS Code!")
- 按
Ctrl + S(Windows/Linux)或Cmd + S(macOS)保存文件。
(2)运行 Python 文件
方法 1:使用 VS Code 终端
- 按
Ctrl + ~(Windows/Linux)或Cmd + ~(macOS)打开终端。 - 输入:
python hello.py
或
python3 hello.py
- 如果看到
Hello, Python in VS Code!,说明运行成功!
方法 2:使用 “Run Python File” 按钮
- 在
hello.py文件中,点击 VS Code 右上角的 “▷ Run Python File” 按钮,Python 代码将在终端中运行。
5. 配置 VS Code 运行环境
(1)启用 Python 虚拟环境(推荐)
建议使用 Python 虚拟环境 来管理 Python 依赖库,避免污染全局 Python 解释器。
- 创建虚拟环境:
python -m venv venv
- 激活虚拟环境:
- Windows:
bash venv\Scripts\activate - macOS/Linux:
bash source venv/bin/activate
- 终端激活后,VS Code 会自动检测并使用该虚拟环境。
6. 配置 Linting 和 Formatting
VS Code 可以帮助格式化 Python 代码,提高可读性。
(1)安装 black 代码格式化工具
pip install black
然后在 VS Code settings.json 文件中添加:
"python.formatting.provider": "black",
(2)启用代码检查(Linting)
可以使用 Pylint 进行代码错误检测:
pip install pylint
然后在 VS Code settings.json 中添加:
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
7. 配置 Debug(调试 Python 代码)
VS Code 支持 Python 代码的断点调试。
(1)设置调试配置
- 点击左侧 “Run and Debug”(运行和调试)按钮 或按
F5。 - 选择 “Python File” 作为调试配置。
- 在代码行前点击左侧的行号(添加断点)。
- 按
F5运行,可以在 VS Code Debug 界面查看变量值。
8. 其他 Python 扩展
可以安装一些 Python 相关扩展,提高开发效率:
- Jupyter(支持 Jupyter Notebook)
- Python Docstring Generator(自动生成文档字符串)
- Python Test Explorer(测试框架)
9. 解决常见问题
(1)找不到 Python 解释器
如果 VS Code 提示 "Python interpreter not found",尝试:
- 在终端运行
python --version确保 Python 已安装。 - 使用
Ctrl + Shift + P选择Python: Select Interpreter,手动选择 Python 解释器路径。
(2)终端 python 命令未识别
在 Windows 上,可能需要手动添加 Python 到 PATH 环境变量:
- 打开
cmd,运行:
setx PATH "%PATH%;C:\Users\你的用户名\AppData\Local\Programs\Python\Python3x\"
- 重新打开 VS Code,然后在终端运行
python --version。
(3)Linting 不工作
- 确保安装了
pylint:
pip install pylint
- 在 VS Code
settings.json中启用:
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
总结
✅ 安装 VS Code 和 Python
✅ 安装 Python 扩展
✅ 配置 Python 解释器
✅ 运行 Python 代码
✅ 启用虚拟环境
✅ 配置 Linting 和 Formatting
✅ 调试 Python 代码
这样,你的 VS Code Python 开发环境 就配置完成了!🎉
更多详细内容请关注其他相关文章!