装的时候运行了一个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
modulepython#.#-distutils
: 提供标准distutils
modulepython#.#-lib2to3
: 提供2to3-#.#
工具 以及标准lib2to3
modulepython#.#-gdbm
: 提供标准dbm.gnu
modulepython#.#-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 9月 4 2021 apt_pkg.cpython-36m-x86_64-linux-gnu.so
-rw-r--r-- 1 root root 8900 9月 4 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 3月 27 2018 dh_pypy -> ../share/dh-python/dh_pypy
lrwxrwxrwx 1 root root 29 3月 27 2018 dh_python3 -> ../share/dh-python/dh_python3
lrwxrwxrwx 1 root root 13 3月 26 14:20 lesspipe -> /bin/lesspipe
lrwxrwxrwx 1 root root 24 3月 25 00:14 pdb3.10 -> ../lib/python3.10/pdb.py
lrwxrwxrwx 1 root root 23 12月 9 05:08 pdb3.6 -> ../lib/python3.6/pdb.py
lrwxrwxrwx 1 root root 23 3月 17 01:28 pdb3.8 -> ../lib/python3.8/pdb.py
-rwxr-xr-x 1 root root 293 5月 1 2021 pip3
lrwxrwxrwx 1 root root 31 3月 26 14:20 py3versions -> ../share/python3/py3versions.py
lrwxrwxrwx 1 root root 26 3月 27 2018 pybuild -> ../share/dh-python/pybuild
lrwxrwxrwx 1 root root 10 3月 26 17:52 python -> python3.10
lrwxrwxrwx 1 root root 10 3月 26 17:53 python3 -> python3.10
-rwxr-xr-x 1 root root 5519352 3月 25 00:14 python3.10
lrwxrwxrwx 1 root root 34 3月 25 00:14 python3.10-config -> x86_64-linux-gnu-python3.10-config
-rwxr-xr-x 2 root root 4526456 12月 9 05:08 python3.6
lrwxrwxrwx 1 root root 33 12月 9 05:08 python3.6-config -> x86_64-linux-gnu-python3.6-config
-rwxr-xr-x 2 root root 4526456 12月 9 05:08 python3.6m
lrwxrwxrwx 1 root root 34 12月 9 05:08 python3.6m-config -> x86_64-linux-gnu-python3.6m-config
-rwxr-xr-x 1 root root 5134032 3月 17 01:28 python3.8
lrwxrwxrwx 1 root root 16 10月 25 2018 python3-config -> python3.6-config
lrwxrwxrwx 1 root root 10 3月 26 14:20 python3m -> python3.6m
lrwxrwxrwx 1 root root 17 10月 25 2018 python3m-config -> python3.6m-config
lrwxrwxrwx 1 root root 9 3月 26 14:20 python3.nak -> python3.6
-rwxr-xr-x 1 root root 3246 3月 25 00:14 x86_64-linux-gnu-python3.10-config
lrwxrwxrwx 1 root root 34 12月 9 05:08 x86_64-linux-gnu-python3.6-config -> x86_64-linux-gnu-python3.6m-config
-rwxr-xr-x 1 root root 3283 12月 9 05:08 x86_64-linux-gnu-python3.6m-config
lrwxrwxrwx 1 root root 33 10月 25 2018 x86_64-linux-gnu-python3-config -> x86_64-linux-gnu-python3.6-config
lrwxrwxrwx 1 root root 34 10月 25 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)