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

7qhs6swi  于 2023-10-16  发布在  Linux
关注(0)|答案(7)|浏览(120)

前几天我决定让python命令默认启动python3而不是python 2。
所以我做了这个:

$ 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

这一切都奏效了。Great!:)

$ python -V
Python 3.5.2

但没过多久我就意识到我在安装和删除python包时破坏了apt/aptitude,因为apt期待的是python 2。
这就是发生的事。

$ 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)

最后我猜它希望python 2作为默认值,所以我撤销了我的更改,如下所示:

$ 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。
有谁能告诉我如何才能做到这一点,而不打破?
我的系统是运行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

(It实际上是一个手臂v8)

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

t1rydlwq1#

取代

[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

[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
并确保在PATH中/usr/local/bin/usr/bin之前。

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

通过添加以下内容来确保始终如此

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

~/.bashrc文件的末尾。通常建议在PATH环境变量前面加上自定义bin文件夹(如/usr/local/bin/opt/<some install>/bin),以确保在默认系统文件之前找到自定义文件。

kmbjn2e3

kmbjn2e32#

根据Debian策略,python指的是Python 2,python3指的是Python 3。不要试图改变整个系统,否则你会陷入你已经发现的那种麻烦。
虚拟环境允许您使用任何版本的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

一个常见的做法是为您所做的每个项目提供一个单独的环境;但是如果你想让它看起来像是对你自己的登录有效的系统范围,你可以将激活节添加到你的.profile或类似的文件中。

nc1teljy

nc1teljy3#

对于任何在2021年或之后发现这个问题的人来说,几乎所有早期的答案都已经过时了。
/usr/bin/python指向Python 3是完全正确的,现在也是可以预期的。Python 2没有更新安全补丁,因此任何仍在使用它的系统都应该升级为使用Python 3作为系统Python。任何现代发行版都应该已经解决了将系统Python升级到Python 3时可能出现的不兼容问题。

bwntbbo3

bwntbbo34#

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

$ 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*
aiqt4smr

aiqt4smr5#

不知何故,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.
o0lyfsai

o0lyfsai6#

我已经编辑了/home/user/.bashrc,

alias python27=/usr/bin/python2.7
alias python31=/usr/bin/python3.10

在Linux ubuntu 20.xx上。我把它加到了文件的最后。
这会将python 27绑定到你想要的版本。

python27 pythonscrypt.py

测试样品,

python27 --version

但是我想知道Python和python3是否应该指向/usr/local/python3.9?
我认为习惯上2.7-2.8是python,3.9.x是python3。
从上面的帖子更新替代品对我来说有点不稳定,所以必须通过将默认值设置为正常来回溯一些。也尝试了anaconda,有些部分在python和anaconda版本之间变得有点混乱。
但我认为编辑.bashrc然后退出终端并重新登录是2021年的最佳解决方案。

保存更改后,请不要忘记退出终端并重新登录!

另一种技术是在python3中使用envs。

yqhsw0fo

yqhsw0fo7#

作为对上面完整解释的补充,这里有一个小脚本来添加所有python版本,这样你就可以复制粘贴了。

i=0 ; for p in /usr/bin/python*.* ; do
  update-alternatives --install /usr/bin/python python $p $((5 + i))
  i=$((i+1))
done
update-alternatives --config python

相关问题