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

2024-09-27 20:17:49 发布

您现在位置:Python中文网/ 问答频道 /正文

前几天,我决定让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更新替代品——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) ...

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

有谁能告诉我,我怎样才能做到这一点,而不打破apt?

我的系统是一个运行Ubuntu的树莓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"

Tags: installedandtopackagebinmodeusrsudo
3条回答

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

替换

[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

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

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

通过添加

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

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

不知怎的,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.

相关问题 更多 >

    热门问题