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

Wil*_*ill 17 python linux ubuntu debian apt

前几天,我决定让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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

而且一切正常。大!:)

$ python -V
Python 3.5.2
Run Code Online (Sandbox Code Playgroud)

但是不久之后,我就意识到在安装和删除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)
Run Code Online (Sandbox Code Playgroud)

最终,我猜想它希望将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
Run Code Online (Sandbox Code Playgroud)

然后再次工作

$ 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) ...
Run Code Online (Sandbox Code Playgroud)

因此,我不得不将其保留为默认值python 2,但我使用python 3开发,因此希望我的系统在运行python和闲置时默认为python 3。

谁能告诉我如何做到这一点而又不至于跌倒?

我的系统是运行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
Run Code Online (Sandbox Code Playgroud)

(实际上是手臂v8)

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.2 LTS"
Run Code Online (Sandbox Code Playgroud)

Dai*_*ood 23

对于 2021 年或以后发现这个问题的人来说,几乎所有早期的答案都已经过时了。

指向 Python 3是完全可以的,也是现在所期望的/usr/bin/python。Python 2 没有通过安全修复程序进行更新,因此任何仍在使用它的系统都应该升级为使用 Python 3 作为系统 Python。任何现代发行版都应该已经解决了将系统 Python 升级到 Python 3 时潜在的不兼容性问题。

  • 所提出的问题不是关于 Python 3 版本之间的差异;而是关于 Python 3 版本之间的差异。它具体是关于让“python”运行Python 3,*无需*从Python 2升级系统。对于另一个问题,这里没有“正确的答案”,这并不奇怪。 (2认同)
  • 事实是,得票最高的答案的第一句话是错误的,并且大多数答案都会破坏现代系统。我的答案现在基本上是正确的,并且随着时间的推移只会变得更加迂腐地正确。 (2认同)

tri*_*eee 14

根据Debian策略,python指的是Python 2和python3Python3。请不要尝试在整个系统范围内进行更改,否则您将遇到已经发现的麻烦。

虚拟环境允许您使用任何版本的Python和所需的任何库运行隔离的Python安装,而不会弄乱系统的Python安装。

在最新的Python 3中,它venv是标准库的一部分;对于旧版本,可能需要安装python3-venv或类似的软件包。

$HOME~$ python --version
Python 2.7.11

$HOME~$ python3 -m venv myenv
... stuff happens ...

$HOME~$ . ./myenv/bin/activate

(myenv) $HOME~$ type python   # "type" is preferred over which; see POSIX
python is /home/you/myenv/bin/python

(myenv) $HOME~$ python --version
Python 3.5.1
Run Code Online (Sandbox Code Playgroud)

无论如何,通常的做法是为您从事的每个项目都有一个单独的环境。但是,如果您希望这样看起来对您自己的登录有效,那么可以将激活节添加到您的登录名.profile或类似物中。

  • 现在这已经有点过时了;不同的发行版对此的版本略有不同。例如,在 Ubuntu 上,“python”现在可以引用“python2”或“python3”(或者,我猜两者都不是),并且系统实用程序必须拼写“python2”或“python3”。 (2认同)
  • 这不是问题的答案,而是_alternative_。支持和反对使用 venv 都有充分的理由。我的团队选择不这样做 (2认同)

小智 5

更换

[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
Run Code Online (Sandbox Code Playgroud)

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

[bash:~] $ sudo update-alternatives --install /usr/local/bin/python \
/usr/bin/python3.5
Run Code Online (Sandbox Code Playgroud)

例如安装/usr/local/bin而不是/usr/bin

并确保PATH中的/usr/local/bin之前/usr/bin

[bash:~] $ echo $PATH
/usr/local/bin:/usr/bin:/bin
Run Code Online (Sandbox Code Playgroud)

通过添加来确保始终如此

export PATH=/usr/local/bin:$PATH
Run Code Online (Sandbox Code Playgroud)

~/.bashrc文件末尾。通常建议PATH使用自定义bin文件夹为环境变量加上前缀,例如/usr/local/bin或,/opt/<some install>/bin以确保在默认系统设置之前找到自定义设置。

  • 我不赞成这样做,因为它可能在某些情况下有效。但实际上,**不要这样做。** (2认同)
  • @tripleee 为什么不呢?我希望系统级 python 不会引用用户的 $PATH 来选择 python 可执行文件。当然这些只是使用 /usr/bin/python 和 /usr/bin/python3,对吗?我看不出引入用户级别更改并向 /usr/local/bin 添加符号链接会如何导致问题。 (2认同)

Seb*_*bMa 5

因为我不想破坏任何东西,所以我这样做是为了能够使用比 Python v3.4 更新的 Python3 版本:

$ sudo update-alternatives --install /usr/local/bin/python3 python3 /usr/bin/python3.6 1
update-alternatives: using /usr/bin/python3.6 to provide /usr/local/bin/python3 (python3) in auto mode
$ sudo update-alternatives --install /usr/local/bin/python3 python3 /usr/bin/python3.7 2
update-alternatives: using /usr/bin/python3.7 to provide /usr/local/bin/python3 (python3) in auto mode
$ update-alternatives --list python3
/usr/bin/python3.6
/usr/bin/python3.7
$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/local/bin/python3).

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

Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python3.6 to provide /usr/local/bin/python3 (python3) in manual mode
$ ls -l /usr/local/bin/python3 /etc/alternatives/python3 
lrwxrwxrwx 1 root root 18 2019-05-03 02:59:03 /etc/alternatives/python3 -> /usr/bin/python3.6*
lrwxrwxrwx 1 root root 25 2019-05-03 02:58:53 /usr/local/bin/python3 -> /etc/alternatives/python3*
Run Code Online (Sandbox Code Playgroud)


Wil*_*ill 4

不知何故,python 3 回来了(经过一些更新?),并导致 apt 更新出现大问题,所以我决定从替代方案中完全删除 python 3:

root:~# python -V
Python 3.5.2

root:~# 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


root:~# update-alternatives --remove python /usr/bin/python3.5

root:~# update-alternatives --config python
There is 1 choice for the alternative python (providing /usr/bin/python).

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

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


root:~# python -V
Python 2.7.12

root:~# update-alternatives --config python
There is only one alternative in link group python (providing /usr/bin/python): /usr/bin/python2.7
Nothing to configure.
Run Code Online (Sandbox Code Playgroud)

  • 事实上,“python3”不是“python”的有效替代品,因为它“需要”引用 Python 2。 (2认同)