如何更新替代Python 3而不打破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 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 

然后再次工作

 $ 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 3当我运行python和空闲。

任何人都可以告诉我,我怎么能达到这一点,而不打破?

我的系统是运行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 

(这实际上是一个armV8)

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

谢谢!

根据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 \ /usr/bin/python2.7 2 [bash:~] $ sudo update-alternatives --install /usr/local/bin/python \ /usr/bin/python3.5 

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

并确保/usr/local/bin /usr/bin位于PATH中的/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.