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

新手上路,请多包涵

前几天我决定让 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"

原文由 Will 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
1 个回答

根据 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 或类似内容。

原文由 tripleee 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题