如何在不破坏 apt 的情况下更新 Python 3 的替代品?

2023-11-29

有一天,我决定让 python 命令默认启动 python3 而不是 python2。

所以我这样做了:

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 2

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 3

$ sudo update-alternatives --config python

$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.5   3         auto mode
  1            /usr/bin/python2.7   2         manual mode
  2            /usr/bin/python3.5   3         manual mode

Press <enter> to keep the current choice[*], or type selection number: 0

这一切都奏效了。伟大的! :)

$ python -V
Python 3.5.2

但没过多久,我就意识到在安装和删除 python 包时我已经破坏了 apt/aptitude,因为 apt 期待 python2 它发生了。

这就是发生的事情。

$ sudo apt remove  python-samba
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  samba-libs
Use 'sudo apt autoremove' to remove it.
The following packages will be REMOVED:
  python-samba
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 5,790 kB disk space will be freed.
Do you want to continue? [Y/n] 
(Reading database ... 187285 files and directories currently installed.)
Removing python-samba (2:4.3.11+dfsg-0ubuntu0.16.04.5) ...
  File "/usr/bin/pyclean", line 63
    except (IOError, OSError), e:
                             ^
SyntaxError: invalid syntax
dpkg: error processing package python-samba (--remove):
 subprocess installed pre-removal script returned error exit status 1
Traceback (most recent call last):
  File "/usr/bin/pycompile", line 35, in <module>
    from debpython.version import SUPPORTED, debsorted, vrepr, \
  File "/usr/share/python/debpython/version.py", line 24, in <module>
    from ConfigParser import SafeConfigParser
ImportError: No module named 'ConfigParser'
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
 python-samba
E: Sub-process /usr/bin/dpkg returned an error code (1)

最终我猜想它想要 python2 作为默认值,所以我取消了我的更改,如下所示:

$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.5   3         auto mode
  1            /usr/bin/python2.7   2         manual mode
  2            /usr/bin/python3.5   3         manual mode

Press <enter> to keep the current choice[*], or type selection number: 1

$ python -V
Python 2.7.12

然后apt又工作了

$ sudo apt remove  python-samba
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  samba-libs
Use 'sudo apt autoremove' to remove it.
The following packages will be REMOVED:
  python-samba
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
1 not fully installed or removed.
After this operation, 5,790 kB disk space will be freed.
Do you want to continue? [Y/n] 
(Reading database ... 187285 files and directories currently installed.)
Removing python-samba (2:4.3.11+dfsg-0ubuntu0.16.04.5) ...

所以我不得不将其保留为默认为 python 2,但我在 python 3 中开发,因此希望我的系统在运行 python 和空闲时默认为 python 3。

谁能告诉我如何在不破坏 apt 的情况下实现这一目标?

我的系统是运行 Ubuntu 的 Raspberry Pi 3B:

Linux mymachine 4.4.38-v7+ #938 SMP Thu Dec 15 15:22:21 GMT 2016 armv7l armv7l armv7l GNU/Linux

(实际上是arm v8)

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.2 LTS"

replace

[bash:~] $ sudo update-alternatives --install /usr/bin/python python \
/usr/bin/python2.7 2

[bash:~] $ sudo update-alternatives --install /usr/bin/python python \
/usr/bin/python3.5 3

with

[bash:~] $ sudo update-alternatives --install /usr/local/bin/python python \
/usr/bin/python2.7 2

[bash:~] $ sudo update-alternatives --install /usr/local/bin/python python \
/usr/bin/python3.5 3

例如安装到/usr/local/bin代替/usr/bin.

并确保/usr/local/bin是在之前/usr/bin在路径中。

i.e.

[bash:~] $ echo $PATH
/usr/local/bin:/usr/bin:/bin

通过添加确保始终如此

export PATH=/usr/local/bin:$PATH

到你的结束~/.bashrc文件。前缀PATH具有自定义 bin 文件夹的环境变量,例如/usr/local/bin or /opt/<some install>/bin通常建议确保在默认系统自定义项之前找到自定义项。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在不破坏 apt 的情况下更新 Python 3 的替代品? 的相关文章

随机推荐