目录
一、Python安装全流程(Windows/Mac/Linux)
[1. 下载官方安装包](#1. 下载官方安装包)
[2. 详细安装步骤(以Windows为例)](#2. 详细安装步骤(以Windows为例))
[3. 环境变量配置(Mac/Linux)](#3. 环境变量配置(Mac/Linux))
[1. 使用venv(Python内置)](#1. 使用venv(Python内置))
[2. 使用conda(推荐数据科学方向)](#2. 使用conda(推荐数据科学方向))
[1. IDE选择](#1. IDE选择)
[2. VS Code配置指南](#2. VS Code配置指南)
[1. python命令无效?](#1. python命令无效?)
[2. pip安装包速度慢?](#2. pip安装包速度慢?)
[3. 多版本Python管理](#3. 多版本Python管理)
一、Python安装全流程(Windows/Mac/Linux)
1. 下载官方安装包
-
版本选择建议 :推荐Python 3.10+(勾选
Add Python to PATH
)
2. 详细安装步骤(以Windows为例)
-
双击安装包
-
勾选 "Install launcher for all users" 和 "Add Python to PATH"
-
选择自定义安装 → 确保所有可选组件被勾选
-
安装完成后验证:
bash
python --version pip --version
3. 环境变量配置(Mac/Linux)
bash
# 查看Python路径
which python3
# 永久添加环境变量
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
二、虚拟环境管理(关键!)
为什么需要虚拟环境?
-
隔离项目依赖
-
避免版本冲突
-
便于依赖迁移
1. 使用venv(Python内置)
bash
# 创建环境
python -m venv myenv
# 激活环境
# Windows:
myenv\Scripts\activate.bat
# Mac/Linux:
source myenv/bin/activate
# 退出环境
deactivate
2. 使用conda(推荐数据科学方向)
bash
# 安装Miniconda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
# 创建环境
conda create -n myenv python=3.10
# 激活环境
conda activate myenv
三、开发工具推荐与配置
1. IDE选择
工具 | 特点 | 适用场景 |
---|---|---|
PyCharm | 功能全面,调试方便 | 大型项目开发 |
VS Code | 轻量级,插件丰富 | 通用开发 |
Jupyter | 交互式编程 | 数据分析 |
2. VS Code配置指南
-
安装Python扩展
-
设置Python解释器路径(Ctrl+Shift+P → "Python: Select Interpreter")
-
推荐安装插件:
-
Pylance(代码提示)
-
Python Test Explorer(测试支持)
-
Jupyter(笔记本支持)
-
四、常见问题解决方案
1. python
命令无效?
-
重新安装并勾选
Add Python to PATH
-
手动添加环境变量:
-
Windows:
系统属性 → 高级 → 环境变量 → 编辑Path
-
Mac/Linux:检查
~/.bash_profile
配置
-
2. pip安装包速度慢?
bash
# 使用国内镜像源
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
# 永久配置
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
3. 多版本Python管理
bash
# Windows使用py启动器
py -3.10 -m pip install package # 指定Python3.10
# Linux使用update-alternatives
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
五、最佳实践总结
-
始终使用虚拟环境
-
项目根目录添加
requirements.txt
bash
pip freeze > requirements.txt pip install -r requirements.txt
-
定期更新核心包
bash
pip install --upgrade pip setuptools wheel
学习资源: