ubuntu系统python版本引发的血案 2021-12-05

博主在尝试将Ubuntu 16.04服务器上的Python从3.5升级到3.10的过程中遇到一系列问题,包括pip错误、依赖冲突和模块缺失。在尝试卸载Python3.10并安装3.9时,遇到了pip不可用和typing模块缺失的问题。在修复过程中,错误地执行了`apt-get autoremove`导致更多问题,如apt更新和升级时的依赖错误。最终,通过`dpkg --configure -a`、强制覆盖安装和重置python软链接解决了部分问题,但仍有部分依赖未修复。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今日闲的蛋疼,想要升级一下ubuntu16.04服务器上的python版本(现在装的是python3.5,但心血来潮想装python3.10体验一下),结果引发了一连串血案。

  1. python3.10的安装:
    1. 添加ppa:sudo add-apt-repository ppa:deadsnakes/ppa,执行后回车等一会;
    2. sudo apt update
    3. sudo apt install python3.10
  2. python3.10的问题及卸载:
    1. pip的时候出问题:AttributeError: module 'collections' has no attribute 'MutableMapping',参考了 https://github.com/sabnzbd/sabnzbd/issues/1971 的python3.10 -m pip install babelfish -U,但是pip install就执行不了,所以不行;
    2. 尝试删除,千万不要这样做!!!! 用了 https://blog.csdn.net/p1279030826/article/details/116564195 的sudo apt-get purge --auto-remove python3.10,还确认了。千万不要这样做!!!!。都是因为直接执行apt remove python3.10之后还能用python3.10。(实际上,直接remove python3.10-minimal就能解决这个问题)。关于autoremove的说明在 https://blog.csdn.net/x2222265/article/details/88526690 有;
    3. 后来又尝试安装python3.9,但是python3.9的pip仍然不可用。
  3. apt出问题:
    1. 继续手欠,尝试了apt的update和upgrade,发现出了一个大问题:依赖出现问题,并且提示让我使用apt -f install命令。但使用该命令后仍然有各种依赖冲突,并且提示:E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.E: Unable to correct dependencies,就像 https://superuser.com/questions/1386209/how-to-solve-this-dependencies-apt-fix-broken-install 所说;并且自动升级到了python3.6,python3.6中会出现python3 ModuleNotFoundError: No module named 'typing'问题。
    2. 解决方案:
      1. https://blog.csdn.net/weixin_42156097/article/details/103805063 给出用aptitude安装,但是并没有aptitude并且安装aptitude的时候仍是报之前的错;
      2. https://askubuntu.com/questions/124845/eerror-pkgproblemresolverresolve-generated-breaks-this-may-be-caused-by-hel 给出用update和upgrade,但是仍没有效果,upgrade的时候报之前的错误;
      3. https://stackoverflow.com/questions/30768828/e-error-pkgproblemresolverresolve-generated-breaks-this-may-be-caused-by-he 给出sudo dpkg --configure -a,并且新开一个ssh链接,执行update和upgrade有效。这个过程中仍有一些问题,如下所示:
        1. 问题1:https://askubuntu.com/questions/148383/how-to-resolve-dpkg-error-processing-var-cache-apt-archives-python-apport-2-0 出现类似dpkg: error processing /var/cache/apt/archives/python-apport_2.0.1-0ubuntu9_all.deb的提示。解决:用sudo dpkg -i --force-overwrite <filename>
        2. 问题2:在dpkg执行过程中,要删除一个东西的时候出现python3 ModuleNotFoundError: No module named 'typing'。问题是在于:这里用到了python,但python用的是后来装的python版本,所以直接重新链接python(将/usr/lib/python3换成python3.5)
          1. 提示全文:
             dell@slave1:~$ sudo apt-get -f install
             Reading package lists... Done
             Building dependency tree       
             Reading state information... Done
             Correcting dependencies... Done
             The following packages were automatically installed and are no longer required:
               distro-info libllvm4.0 libllvm5.0 libpython3-dev libpython3.5-dev libpython3.9-minimal libqt5x11extras5 python3-distro-info python3-yaml python3.5-dev
               python3.9-minimal qml-module-qtquick-controls qml-module-qtquick-dialogs qml-module-qtquick-privatewidgets qtdeclarative5-controls-plugin
               qtdeclarative5-dialogs-plugin
             Use 'sudo apt autoremove' to remove them.
             The following additional packages will be installed:
               libnettle6 libpython3.6-stdlib
             The following packages will be REMOVED:
               ubuntu-advantage-tools
             The following NEW packages will be installed:
               libnettle6 libpython3.6-stdlib
             0 upgraded, 2 newly installed, 1 to remove and 0 not upgraded.
             2 not fully installed or removed.
             Need to get 0 B/2,313 kB of archives.
             After this operation, 7,973 kB of additional disk space will be used.
             Do you want to continue? [Y/n] Y
             (Reading database ... 307863 files and directories currently installed.)
             Removing ubuntu-advantage-tools (27.4.1~16.04.1) ...
             Traceback (most recent call last):
               File "<string>", line 2, in <module>
               File "/usr/lib/python3/dist-packages/uaclient/apt.py", line 7, in <module>
                 from typing import Dict, List, Optional
             ModuleNotFoundError: No module named 'typing'
             dpkg: error processing package ubuntu-advantage-tools (--remove):
              subprocess installed pre-removal script returned error exit status 1
             Errors were encountered while processing:
              ubuntu-advantage-tools
             E: Sub-process /usr/bin/dpkg returned an error code (1)
             ```
          
  4. python升级到3.6的后遗症
    1. 之前upgrade安装了python3.6,但是pip还是之前的python3.9的pip。所以删掉/usr/local/bin/pip并通过sudo cp /usr/local/bin/pip3.6 /usr/local/bin/pip将pip重新指定定了一次;
    2. 用pip的时候出现ImportError: cannot import name 'sysconfig' from 'distutils',所以用 https://www.codeleading.com/article/11985788299/ 的方法,利用sudo apt-get install python3.6-distutils修复。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值