温馨提示×

如何配置ubuntu的python

小樊
41
2026-10-13 04:39:45
栏目: 编程语言

下面按常见需求给你一个比较完整的 Ubuntu Python 配置指南,从系统 Python 到开发环境,你可以按需取用。


一、Ubuntu 自带 Python(先看版本)

Ubuntu 通常自带 Python 3:

python3 --version
which python3

⚠️ 不要删除系统 Python,很多系统工具依赖它。


二、安装 / 升级 Python(推荐方式)

1. 使用 apt 安装(最简单)

sudo apt update
sudo apt install python3 python3-pip python3-venv

查看版本:

python3 -V
pip3 -V

2. 安装多个 Python 版本(如 3.10 / 3.11)

sudo apt install python3.10 python3.11

切换默认(可选):

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2
sudo update-alternatives --config python3

3. 使用 deadsnakes PPA(版本更全)

sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.12

三、配置 pip(非常重要)

升级 pip

python3 -m pip install --upgrade pip

配置国内镜像(可选,国内更快)

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

四、使用虚拟环境(强烈推荐)

创建虚拟环境

python3 -m venv venv

激活环境

source venv/bin/activate

退出

deactivate

五、python 命令指向 python3(可选)

echo "alias python=python3" >> ~/.bashrc
source ~/.bashrc

(不建议改系统 /usr/bin/python)


六、开发工具推荐

1. 安装 ipython

pip install ipython

2. 安装常用包

pip install numpy pandas requests flask django

3. 编辑器

  • VS Code(推荐)
  • PyCharm

七、常见问题

pip 不可用?

sudo apt install python3-pip

权限错误?

✅ 用虚拟环境
❌ 不要用 sudo pip


如果你告诉我:

  • Ubuntu 版本
  • 是服务器 / 桌面
  • 用于学习 / 开发 / 部署

我可以给你更精确的配置方案。

0 踩