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

如何在不破坏apt的情况下更新Python 3的替代方案?,python,linux,ubuntu,debian,apt,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 $ sudo update-alternatives --config pyt

前几天,我决定让命令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软件包时,我已经破坏了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,但我在Python3中开发,因此希望在运行python和空闲时,我的系统默认为Python3

有人能告诉我如何在不破坏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)


不知怎的,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.
root:~#python-V
Python 3.5.2
root:~#更新备选方案--配置python
替代python有两种选择(提供/usr/bin/python)。
选择路径优先级状态
------------------------------------------------------------
*0/usr/bin/python3.5 3自动模式
1/usr/bin/python2.7 2手动模式
2/usr/bin/python3.5 3手动模式
root:~#更新备选方案--删除python/usr/bin/python3.5
root:~#更新备选方案--配置python
备选python有1种选择(提供/usr/bin/python)。
选择路径优先级状态
------------------------------------------------------------
0/usr/bin/python2.7 2自动模式
*1/usr/bin/python2.7 2手动模式
按可保留当前选项[*],或类型选择编号:0
root:~#python-V
Python 2.7.12
root:~#更新备选方案--配置python
在链接组python中只有一种选择(提供/usr/bin/python):/usr/bin/python2.7
没有要配置的内容。

根据Debian策略,
python
指的是python2,而
python3
指的是python3。不要试图改变整个系统,否则你会遇到你已经发现的那种麻烦

虚拟环境允许您使用任何版本的Python和所需的任何库运行独立的Python安装,而不会影响系统Python安装

在最新的Python3中,
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
或类似文件中。

替换

[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

e、 g.安装到
/usr/local/bin
而不是
/usr/bin

并确保路径中的
/usr/local/bin
/usr/bin
之前

i、 e

通过添加

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

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

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


请将
python
指向python2,否则您会遇到很多麻烦是的,谢谢,我也这么想了。:)这个问题的答案需要更新。我目前正在运行默认使用python3的Debian(通过更新选项),我没有遇到任何问题。如果将来出现故障,我会发表评论,但趋势是它将成为新的默认值。事实上,
python3
对于
python
来说不是一个有效的替代方案,因为它需要引用python 2。最初设置它时,您告诉系统python3的优先级高于python2。然后手动将其设置为Python2。似乎有什么东西把它放回了自动模式,所以选择了python3。因此,删除python3可能是正确的做法,但将其设置为较低的优先级也会产生类似的效果。如果是我,我可能会从更新选项中删除这两个条目,以恢复系统的启动方式。我不投反对票,因为这在某些情况下可能有效;但真的,不要这样做。@tripleee为什么不呢?我希望系统级python不会引用用户的$PATH来选择python可执行文件。当然,它们只是在使用/usr/bin/python和/usr/bin/python3,对吗?我看不出引入用户级更改并将符号链接添加到/usr/local/bin会造成什么问题。因为
更新替代方案
是错误的,所以永远不要用它来将
python
版本2替换为
python3
。这个问题描述了一些记录良好的症状。因此,这取决于Ubuntu中的软件如何查找
python
可执行文件。我的假设是,系统进程不只是查看$PATH,因为它依赖于用户。而
updatealternations
所做的唯一事情就是在
/usr/local/bin/python
上创建一个符号链接。
/usr/bin
中没有任何更改。只要
apt
(或类似产品)正在寻找
/usr/bin/python
,而不仅仅是
python
,就应该可以了。我已经用apt测试过了,看起来还不错。但这并不能保证它能与所有系统软件一起工作!我怀疑它是否能随身携带。您正在安装一个从
/usr/local/bin/python
/etc/alternations/python
的符号链接,但后者已用于指向系统python,并且正在更改
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.
$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
[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
[bash:~] $ echo $PATH
/usr/local/bin:/usr/bin:/bin
export PATH=/usr/local/bin:$PATH
$ 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*