ubuntu 安装python3.10和对应版本的pip

本文档介绍了如何在Ubuntu系统中安装Python3.10及其对应的pip。过程中需注意避免因误操作导致系统问题,如安装依赖、添加Python版本库、解决模块缺失错误等。详细步骤包括安装通用依赖、添加Python版本库、安装Python3.10及其依赖、安装pip,并提供了解决No module named 'apt_pkg'和'collections.MutableMapping'错误的方法。
部署运行你感兴趣的模型镜像

装的时候运行了一个apt autoremove python3.6 直接搞崩了系统上不了网了,直接重装了系统,还是要小心啊。
sudo apt-get install --reinstall ubuntu-desktop

1. 安装python3.10 及 对应的pip

需要注意的是如果直接apt-get install python3-pip的话装的不是对应的3.10版本的,可能会导致多个问题。

1.1安装通用软件依赖

sudo apt install software-properties-common

1.2 添加Ubuntu的python版本 所在的库:

sudo add-apt-repository ppa:deadsnakes/ppa

执行后看一下输出,输出会告诉我们需要做什么:

ltt@ltt-E440:~$ sudo add-apt-repository ppa:deadsnakes/ppa
 This PPA contains more recent Python versions packaged for Ubuntu.

Disclaimer: there's no guarantee of timely updates in case of security problems or other issues. If you want to use them in a security-or-otherwise-critical environment (say, on a production server), you do so at your own risk.

Update Note
===========
Please use this repository instead of ppa:fkrull/deadsnakes.

Reporting Issues
================

Issues can be reported in the master issue tracker at:
https://github.com/deadsnakes/issues/issues

Supported Ubuntu and Python Versions
====================================

- Ubuntu 18.04 (bionic) Python2.3 - Python 2.6, Python 3.1 - Python 3.5, Python3.7 - Python3.11
- Ubuntu 20.04 (focal) Python3.5 - Python3.7, Python3.9 - Python3.11
- Note: Python2.7 (all), Python 3.6 (bionic), Python 3.8 (focal) are not provided by deadsnakes as upstream ubuntu provides those packages.
- Note: for focal, older python versions require libssl1.0.x so they are not currently built

The packages may also work on other versions of Ubuntu or Debian, but that is not tested or supported.

Packages
========

The packages provided here are loosely based on the debian upstream packages with some modifications to make them more usable as non-default pythons and on ubuntu.  As such, the packages follow debian's patterns and often do not include a full python distribution with just `apt install python#.#`.  Here is a list of packages that may be useful along with the default install:

- `python#.#-dev`: includes development headers for building C extensions
- `python#.#-venv`: provides the standard library `venv` module
- `python#.#-distutils`: provides the standard library `distutils` module
- `python#.#-lib2to3`: provides the `2to3-#.#` utility as well as the standard library `lib2to3` module
- `python#.#-gdbm`: provides the standard library `dbm.gnu` module
- `python#.#-tk`: provides the standard library `tkinter` module

可以看到输出告诉了我们系统支持的python版本,最后一个区间是python3.7到python3.11,我官网看的明明最新是3.10.4,所以我们装的是python3.10

1.3. 安装支持的python版本Supported Ubuntu and Python Versions

sudo apt-get install python3.10

1.4 安装python 依赖包

按照输出的提示安装:

  • python#.#-dev: 构建 C extensions 的依赖包(用于开发)
  • python#.#-venv: 提供标准的 venv module
  • python#.#-distutils: 提供标准 distutils module
  • python#.#-lib2to3: 提供 2to3-#.# 工具 以及标准 lib2to3 module
  • python#.#-gdbm: 提供标准 dbm.gnu module
  • python#.#-tk: 提供标准 tkinter module

把#.# 换成对应的版本就可以了,如果你没有直接根据输出安装,而是根据查找到的博客等的执行了 apt-get install python3-dev的话(比如我),可以加--reinstall 选项重新安装,不安装这些包的话就会报错,比如 No Module named 'distutils.cmd’等。

sudo apt-get install --reinstall python3.10-dev
sudo apt-get install --reinstall python3.10-venv
sudo apt-get install --reinstall python3.10-distutils
sudo apt-get install --reinstall python3.10-lib2to3
sudo apt-get install --reinstall python3.10-gdbm
sudo apt-get install --reinstall python3.10-tk

1.5 安装pip

这里需要注意的是通过 sudo apt install python3-pip 安装的话他会自动安装一个python3.6 ,然后pip版本是9.0.1 (python3.6),在这里安装pip通过curl命令安装

curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10

报错的话通常就是1.4小节的依赖包没装。

报错解决

检查一下pip版本:No module named ‘apt_pkg’

如下到/usr/lib/python3/dist-packages目录下 找一下apt_pkg.so 的指向,没有的话创建一个软连接:

ltt@ltt-E440:/usr/lib/python3/dist-packages$ ls -lt | grep apt_pkg
-rw-r--r--  1 root root 346784 94  2021 apt_pkg.cpython-36m-x86_64-linux-gnu.so
-rw-r--r--  1 root root   8900 94  2021 apt_pkg.pyi

ltt@ltt-E440:/usr/lib/python3/dist-packages$ sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

AttributeError: module ‘collections’ has no attribute ‘MutableMapping’

这个错误呢就是因为安装了多个版本的python3导致无法识别的问题,

ltt@ltt-E440:/usr/lib/python3/dist-packages$ python -m pip --version
(中间省略)
AttributeError: module 'collections' has no attribute 'MutableMapping'

对此,是python3的多版本配置的问题,到/usr/bin 目录下:

ltt@ltt-E440:/usr/bin$ ls -la | grep -i "pip\|python"
lrwxrwxrwx  1 root root          26 327  2018 dh_pypy -> ../share/dh-python/dh_pypy
lrwxrwxrwx  1 root root          29 327  2018 dh_python3 -> ../share/dh-python/dh_python3
lrwxrwxrwx  1 root root          13 326 14:20 lesspipe -> /bin/lesspipe
lrwxrwxrwx  1 root root          24 325 00:14 pdb3.10 -> ../lib/python3.10/pdb.py
lrwxrwxrwx  1 root root          23 129 05:08 pdb3.6 -> ../lib/python3.6/pdb.py
lrwxrwxrwx  1 root root          23 317 01:28 pdb3.8 -> ../lib/python3.8/pdb.py
-rwxr-xr-x  1 root root         293 51  2021 pip3
lrwxrwxrwx  1 root root          31 326 14:20 py3versions -> ../share/python3/py3versions.py
lrwxrwxrwx  1 root root          26 327  2018 pybuild -> ../share/dh-python/pybuild
lrwxrwxrwx  1 root root          10 326 17:52 python -> python3.10
lrwxrwxrwx  1 root root          10 326 17:53 python3 -> python3.10
-rwxr-xr-x  1 root root     5519352 325 00:14 python3.10
lrwxrwxrwx  1 root root          34 325 00:14 python3.10-config -> x86_64-linux-gnu-python3.10-config
-rwxr-xr-x  2 root root     4526456 129 05:08 python3.6
lrwxrwxrwx  1 root root          33 129 05:08 python3.6-config -> x86_64-linux-gnu-python3.6-config
-rwxr-xr-x  2 root root     4526456 129 05:08 python3.6m
lrwxrwxrwx  1 root root          34 129 05:08 python3.6m-config -> x86_64-linux-gnu-python3.6m-config
-rwxr-xr-x  1 root root     5134032 317 01:28 python3.8
lrwxrwxrwx  1 root root          16 1025  2018 python3-config -> python3.6-config
lrwxrwxrwx  1 root root          10 326 14:20 python3m -> python3.6m
lrwxrwxrwx  1 root root          17 1025  2018 python3m-config -> python3.6m-config
lrwxrwxrwx  1 root root           9 326 14:20 python3.nak -> python3.6
-rwxr-xr-x  1 root root        3246 325 00:14 x86_64-linux-gnu-python3.10-config
lrwxrwxrwx  1 root root          34 129 05:08 x86_64-linux-gnu-python3.6-config -> x86_64-linux-gnu-python3.6m-config
-rwxr-xr-x  1 root root        3283 129 05:08 x86_64-linux-gnu-python3.6m-config
lrwxrwxrwx  1 root root          33 1025  2018 x86_64-linux-gnu-python3-config -> x86_64-linux-gnu-python3.6-config
lrwxrwxrwx  1 root root          34 1025  2018 x86_64-linux-gnu-python3m-config -> x86_64-linux-gnu-python3.6m-config

可以看到 python3-config 指向 python3.6-config,我们修改成指向 python3.10-config

 sudo mv python3-config python3-config.bak
 sudo ln -s python3.10-config python3-config

检查版本成功,安装了对应3.10的pip版本22.0.4。这样python的安装就没有问题啦。

ltt@ltt-E440:/usr/bin$ python -m pip --version
pip 22.0.4 from /home/ltt/.local/lib/python3.10/site-packages/pip (python 3.10)

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

### Ubuntu安装 Python 3.10 为了在Ubuntu系统上成功安装Python 3.10并配置环境,可以按照如下操作: #### 添加官方PPA 由于Debian/Ubuntu自带的软件源可能不会立即提供最新的Python版本,因此推荐使用第三方维护者deadsnakes提供的PPA来获取较新版本Python解释器。这一步骤能够确保获得稳定且经过测试的Python发行版。 ```bash sudo add-apt-repository ppa:deadsnakes/ppa ``` #### 更新本地包索引 添加PPA之后,应当刷新系统的APT缓存以同步最新可用的软件列表信息。 ```bash sudo apt update ``` #### 安装 Python 3.10 执行下面这条命令即可完成Python 3.10核心组件及其开发工具链的部署工作。 ```bash sudo apt install python3.10 python3.10-dev python3.10-distutils -y ``` #### 设置默认 Python 解释器 如果希望让`python3`命令指向刚刚安装好的Python 3.10,则需利用update-alternatives机制来进行设置。 ```bash sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1 sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2 sudo update-alternatives --config python3 ``` 此时会弹出交互界面让用户选择想要使用的Python版本;输入对应编号后按回车键确认更改[^3]。 #### 配置 Pip 工具 对于Pip而言,默认情况下它会被自动关联到所对应Python版本下。不过也可以手动指定特定版本pipPython 3.10使用。 ```bash curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py sudo python3.10 get-pip.py rm get-pip.py ``` 最后可通过以下方式验证是否正确设置了Python以及Pip: ```bash python3 --version pip3 --version which python3 which pip3 ``` 以上过程涵盖了从准备阶段直到最终检验在内的全部必要环节,使得用户能够在Ubuntu平台上顺利搭建起基于Python 3.10的工作环境[^4]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值