Python 环境配置
本章节将指导你配置 Python 开发环境,包括 Python 安装、虚拟环境管理、包管理器配置等。
📋 配置清单
- Python 安装
- 虚拟环境管理
- 包管理器配置
- 开发工具安装
- 项目配置
- 验证安装
1. 安装 Python
使用 pyenv 安装 (推荐)
macOS/Linux
bash
# 安装 pyenv
curl https://pyenv.run | bashbash
# 添加到 shell 配置
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
# 检查 pyenv 命令是否存在,若不存在则将 pyenv 的 bin 目录加入 PATH 环境变量
# - `command -v pyenv` 检测 pyenv 是否可用
# - `>/dev/null` 将输出重定向到空设备(不显示消息)
# - `||` 如果左侧命令失败(返回非0),则执行右侧命令
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
# 初始化 pyenv 的路径配置(兼容性处理,确保 shell 能正确找到 pyenv)
# - `--path` 选项仅设置必要的 PATH 变量,不加载其他功能
echo 'eval "$(pyenv init --path)"' >> ~/.zshrc
# 完全初始化 pyenv,启用所有功能(包括 shims 和自动补全)
# - 会设置 pyenv 的 shims 目录到 PATH 最前面
# - 启用 rehash 和命令补全等功能
echo 'eval "$(pyenv init -)"' >> ~/.zshrcbash
# 重新加载配置
source ~/.zshrcbash
# 安装最新 Python 版本
pyenv install 3.11.0bash
# 设置全局 Python 版本
pyenv global 3.11.0Windows
powershell
# 使用 Chocolatey 安装 pyenv-win
choco install pyenv-winpowershell
# 安装 Python 版本
pyenv install 3.11.0powershell
# 设置全局 Python 版本
pyenv global 3.11.0直接安装
macOS
bash
# 使用 Homebrew 安装
brew install python@3.11Windows
powershell
# 使用 Chocolatey 安装
choco install pythonLinux
bash
# Ubuntu/Debian
sudo apt update
sudo apt install python3 python3-pip python3-venv2. 配置包管理器
配置 pip 镜像
macOS/Linux
bash
# 设置清华镜像
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simplebash
# 设置阿里云镜像
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/Windows
powershell
# 设置清华镜像
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simplepowershell
# 设置阿里云镜像
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/升级 pip
macOS/Linux
bash
# 升级 pip
python -m pip install --upgrade pipWindows
powershell
# 升级 pip
python -m pip install --upgrade pip3. 虚拟环境管理
安装虚拟环境工具
macOS/Linux
bash
# 安装 virtualenv
pip install virtualenvbash
# 安装 virtualenvwrapper
pip install virtualenvwrapperWindows
powershell
# 安装 virtualenv
pip install virtualenvpowershell
# 安装 virtualenvwrapper-win
pip install virtualenvwrapper-win创建虚拟环境
使用 venv (Python 3.3+)
macOS/Linux
bash
# 创建虚拟环境
python -m venv myprojectbash
# 激活虚拟环境
source myproject/bin/activatebash
# 退出虚拟环境
deactivateWindows
powershell
# 创建虚拟环境
python -m venv myprojectpowershell
# 激活虚拟环境
myproject\Scripts\activatepowershell
# 退出虚拟环境
deactivate使用 virtualenv
macOS/Linux
bash
# 创建虚拟环境
virtualenv myprojectbash
# 激活虚拟环境
source myproject/bin/activatebash
# 退出虚拟环境
deactivateWindows
powershell
# 创建虚拟环境
virtualenv myprojectpowershell
# 激活虚拟环境
myproject\Scripts\activatepowershell
# 退出虚拟环境
deactivate4. 安装开发工具
代码质量工具
macOS/Linux
bash
# 安装 flake8
pip install flake8bash
# 安装 black
pip install blackbash
# 安装 isort
pip install isortbash
# 安装 mypy
pip install mypybash
# 安装 pylint
pip install pylintWindows
powershell
# 安装 flake8
pip install flake8powershell
# 安装 black
pip install blackpowershell
# 安装 isort
pip install isortpowershell
# 安装 mypy
pip install mypypowershell
# 安装 pylint
pip install pylint测试工具
macOS/Linux
bash
# 安装 pytest
pip install pytestbash
# 安装 pytest-cov
pip install pytest-covbash
# 安装 pytest-mock
pip install pytest-mockWindows
powershell
# 安装 pytest
pip install pytestpowershell
# 安装 pytest-cov
pip install pytest-covpowershell
# 安装 pytest-mock
pip install pytest-mock开发框架
macOS/Linux
bash
# 安装 Django
pip install djangobash
# 安装 Flask
pip install flaskbash
# 安装 FastAPI
pip install fastapi uvicornbash
# 安装 Jupyter
pip install jupyterWindows
powershell
# 安装 Django
pip install djangopowershell
# 安装 Flask
pip install flaskpowershell
# 安装 FastAPI
pip install fastapi uvicornpowershell
# 安装 Jupyter
pip install jupyter5. 项目配置
创建 requirements.txt
macOS/Linux
bash
# 创建 requirements.txt
touch requirements.txtbash
# 生成依赖列表
pip freeze > requirements.txtWindows
powershell
# 创建 requirements.txt
New-Item -Path requirements.txt -ItemType Filepowershell
# 生成依赖列表
pip freeze > requirements.txt安装项目依赖
macOS/Linux
bash
# 安装依赖
pip install -r requirements.txtbash
# 安装开发依赖
pip install -r requirements-dev.txtWindows
powershell
# 安装依赖
pip install -r requirements.txtpowershell
# 安装开发依赖
pip install -r requirements-dev.txt6. 环境变量配置
创建 .env 文件
macOS/Linux
bash
# 创建 .env 文件
touch .envbash
# 创建 .env.example 文件
touch .env.exampleWindows
powershell
# 创建 .env 文件
New-Item -Path .env -ItemType Filepowershell
# 创建 .env.example 文件
New-Item -Path .env.example -ItemType File安装 python-dotenv
macOS/Linux
bash
pip install python-dotenvWindows
powershell
pip install python-dotenv7. 调试配置
VS Code 调试配置
创建 .vscode/launch.json 文件:
json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"envFile": "${workspaceFolder}/.env"
}
]
}8. 性能优化
配置 pip 缓存
macOS/Linux
bash
# 查看缓存位置
pip cache dirbash
# 清理缓存
pip cache purgeWindows
powershell
# 查看缓存位置
pip cache dirpowershell
# 清理缓存
pip cache purge使用 pip-tools
macOS/Linux
bash
# 安装 pip-tools
pip install pip-toolsbash
# 编译依赖
pip-compile requirements.inbash
# 同步依赖
pip-sync requirements.txtWindows
powershell
# 安装 pip-tools
pip install pip-toolspowershell
# 编译依赖
pip-compile requirements.inpowershell
# 同步依赖
pip-sync requirements.txt✅ 验证安装
完成安装后,验证以下工具是否正常工作:
macOS/Linux
bash
# 检查 Python
python --versionbash
# 检查 pip
pip --versionbash
# 检查 pyenv
pyenv --versionbash
# 检查虚拟环境
python -m venv --helpbash
# 检查 Django
django-admin --versionbash
# 检查 Flask
flask --versionWindows
powershell
# 检查 Python
python --versionpowershell
# 检查 pip
pip --versionpowershell
# 检查 pyenv
pyenv --versionpowershell
# 检查虚拟环境
python -m venv --helppowershell
# 检查 Django
django-admin --versionpowershell
# 检查 Flask
flask --version🎉 下一步
Python 环境配置完成后,继续配置其他编程语言环境:
遇到问题? 查看 问题排查 页面。