Windows开发环境配置
Windows系统下的完整开发环境搭建指南
准备工作
系统要求
推荐系统:Windows 10 / Windows 11
最低版本:Windows 10 1903及以上
系统类型:64位操作系统
检查系统信息
- 按
Win+I打开设置 - 点击"系统" → "关于"
- 查看Windows版本和系统类型
开启开发者模式(可选)
设置 → 隐私和安全性 → 开发者选项 → 开发人员模式:开启
安装Windows Terminal
为什么需要
Windows Terminal 是微软推出的新一代终端,比传统的 CMD 和 PowerShell 更强大。
安装步骤
方法一:Microsoft Store(推荐)
- 打开 Microsoft Store
- 搜索 "Windows Terminal"
- 点击安装
方法二:GitHub下载
- 访问 https://github.com/microsoft/terminal/releases
- 下载最新版 .msixbundle 文件
- 双击安装
基本配置
// 打开设置(Ctrl + ,),可以配置:
{
"defaultProfile": "{powershell-guid}", // 默认使用PowerShell
"theme": "dark", // 深色主题
"fontSize": 14 // 字体大小
}
安装Python
下载Python
- 访问 https://www.python.org/downloads/
- 点击 "Download Python 3.x.x"(下载最新稳定版)
- 下载完成后运行安装程序
安装步骤
⚠️ 重要:勾选 "Add Python to PATH"
安装界面:
☑️ Add Python 3.x to PATH ← 必须勾选!
☐ Install launcher for all users
点击 "Install Now" 或 "Customize installation"
自定义安装选项(推荐):
Optional Features:
☑️ Documentation
☑️ pip
☑️ tcl/tk and IDLE
☑️ Python test suite
☑️ py launcher
Advanced Options:
☑️ Install for all users
☑️ Add Python to environment variables
☑️ Create shortcuts for installed applications
安装路径:C:\Python3x(简短路径更好)
验证安装
打开 Windows Terminal,输入:
# 检查Python版本
python --version
# 输出示例:Python 3.12.0
# 检查pip版本
pip --version
# 输出示例:pip 23.x.x from ...
# 测试Python
python -c "print('Hello, Python!')"
# 输出:Hello, Python!
配置pip镜像源(提升下载速度)
# 创建pip配置目录
mkdir $env:APPDATA\pip
# 创建配置文件
notepad $env:APPDATA\pip\pip.ini
在打开的记事本中输入:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
保存并关闭。
安装Node.js
下载Node.js
- 访问 https://nodejs.org/
- 下载 LTS(长期支持版)
- 运行安装程序
安装步骤
安装向导:
1. 接受许可协议
2. 选择安装路径(默认即可)
3. 选择组件(默认全选)
4. ☑️ Automatically install necessary tools ← 可选
5. 点击Install完成安装
验证安装
# 检查Node.js版本
node --version
# 输出示例:v20.x.x
# 检查npm版本
npm --version
# 输出示例:10.x.x
# 测试Node.js
node -e "console.log('Hello, Node.js!')"
# 输出:Hello, Node.js!
配置npm镜像源
# 设置淘宝镜像
npm config set registry https://registry.npmmirror.com
# 验证配置
npm config get registry
安装VS Code
下载安装
- 访问 https://code.visualstudio.com/
- 点击 "Download for Windows"
- 运行安装程序
安装选项
安装选项(建议全选):
☑️ 创建桌面快捷方式
☑️ 将"通过Code打开"操作添加到Windows资源管理器文件上下文菜单
☑️ 将"通过Code打开"操作添加到Windows资源管理器目录上下文菜单
☑️ 将Code注册为受支持的文件类型的编辑器
☑️ 添加到PATH(需要重启生效)
必装插件
打开VS Code,按 Ctrl +Shift+ X 打开插件市场,搜索并安装:
| 插件名 | 用途 |
|---|---|
| Chinese (Simplified) | 中文语言包 |
| Python | Python开发支持 |
| Pylance | Python智能提示 |
| JavaScript (ES6) code snippets | JS代码片段 |
| Prettier | 代码格式化 |
| GitLens | Git增强 |
| Material Icon Theme | 文件图标美化 |
基本配置
按 Ctrl + , 打开设置,推荐配置:
{
"editor.fontSize": 14,
"editor.tabSize": 4,
"editor.formatOnSave": true,
"editor.minimap.enabled": false,
"files.autoSave": "afterDelay",
"terminal.integrated.defaultProfile.windows": "PowerShell"
}
安装Git
下载安装
- 访问 https://git-scm.com/download/win
- 下载适合系统的版本(64位/32位)
- 运行安装程序
安装选项详解
1. Select Components(选择组件)
☑️ Windows Explorer integration(右键菜单集成)
☑️ Git LFS(大文件支持)
☑️ Associate .git* configuration files
2. Choosing the default editor(选择默认编辑器)
→ Use Visual Studio Code as Git's default editor
3. Adjusting the name of the initial branch
→ Override the default branch name: main
4. Adjusting your PATH environment
→ Git from the command line and also from 3rd-party software(推荐)
5. Choosing HTTPS transport backend
→ Use the OpenSSL library
6. Configuring the line ending conversions
→ Checkout Windows-style, commit Unix-style line endings
7. Configuring the terminal emulator
→ Use Windows' default console window
8. 其他选项默认即可
验证安装
# 检查Git版本
git --version
# 输出示例:git version 2.x.x.windows.x
配置Git
# 设置用户名和邮箱(用于提交记录)
git config --global user.name "你的名字"
git config --global user.email "你的邮箱@example.com"
# 设置默认分支名为main
git config --global init.defaultBranch main
# 设置VS Code为默认编辑器
git config --global core.editor "code --wait"
# 查看配置
git config --list
安装WSL2(可选但推荐)
什么是WSL
WSL(Windows Subsystem for Linux) 让你在Windows上运行Linux环境。
为什么推荐
- 使用Linux命令和工具
- 更好的开发体验
- 与服务器环境一致
- Docker支持更好
安装步骤
# 以管理员身份打开PowerShell
# 一键安装WSL2和Ubuntu
wsl --install
# 安装完成后重启电脑
重启后:
- 会自动打开Ubuntu终端
- 设置用户名和密码
- 完成初始化
验证安装
# 查看已安装的Linux发行版
wsl --list --verbose
# 进入Linux环境
wsl
VS Code 配合 WSL
- 在VS Code中安装 "WSL" 插件
- 按
F1,输入 "WSL: Connect to WSL" - 现在可以在Windows中开发Linux项目
环境变量配置
什么是环境变量
环境变量 是操作系统用来存储配置信息的机制,告诉系统在哪里找到程序。
查看环境变量
# 查看PATH变量
$env:PATH -split ';'
# 查看所有环境变量
Get-ChildItem Env:
手动添加环境变量
- 搜索"环境变量",点击"编辑系统环境变量"
- 点击"环境变量"按钮
- 在"用户变量"或"系统变量"中找到 Path
- 点击编辑,添加新路径
- 确定保存,重启终端生效
常见问题
问题:命令not recognized
原因:程序路径未添加到PATH
解决:
1. 确认程序安装位置
2. 将该路径添加到PATH
3. 重启终端
开发环境验证
完整检查清单
# 创建测试脚本
# 新建文件 test_env.ps1,内容如下:
Write-Host "=== 开发环境检查 ===" -ForegroundColor Green
Write-Host "`n[Python]" -ForegroundColor Yellow
python --version
pip --version
Write-Host "`n[Node.js]" -ForegroundColor Yellow
node --version
npm --version
Write-Host "`n[Git]" -ForegroundColor Yellow
git --version
Write-Host "`n[VS Code]" -ForegroundColor Yellow
code --version
Write-Host "`n=== 检查完成 ===" -ForegroundColor Green
运行:
.\test_env.ps1
创建第一个Python项目
# 创建项目目录
mkdir ~/Projects/hello-python
cd ~/Projects/hello-python
# 创建虚拟环境
python -m venv venv
# 激活虚拟环境
.\venv\Scripts\Activate.ps1
# 创建主文件
code hello.py
在 hello.py 中输入:
print("Hello, Python!")
print("开发环境配置成功!")
运行:
python hello.py
常见问题解决
PowerShell执行策略
# 问题:无法运行脚本
# 解决:以管理员身份运行
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Python命令无法识别
# 检查Python安装路径
where.exe python
# 如果没有输出,需要手动添加到PATH
# 默认路径:C:\Users\用户名\AppData\Local\Programs\Python\Python3x
pip下载慢
# 临时使用镜像
pip install 包名 -i https://pypi.tuna.tsinghua.edu.cn/simple
Git中文乱码
git config --global core.quotepath false
git config --global gui.encoding utf-8
git config --global i18n.commit.encoding utf-8
git config --global i18n.logoutputencoding utf-8
本章小结
- Windows Terminal:新一代终端,体验更好
- Python:记得勾选"Add to PATH"
- Node.js:下载LTS版本
- VS Code:安装必要插件
- Git:配置用户名和邮箱
- WSL2:可选但推荐,提升开发体验
下一步
环境配置完成!接下来深入了解代码编辑器的使用。
→ 继续阅读:10-代码编辑器选择与配置 → Mac用户:09-Mac开发环境配置