前几天,我决定让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"
根据Debian策略,
python
表示Python 2,python3
表示Python 3。不要试图在整个系统范围内更改此项,否则您将遇到您已经发现的那种麻烦。虚拟环境允许您使用Python的任何版本和所需的任何库运行独立的Python安装,而无需干扰系统Python安装。
对于最新的Python 3,
venv
是标准库的一部分;对于较旧的版本,您可能需要安装python3-venv
或类似的包。一个常见的做法是,无论如何,为您所处理的每个项目都有一个单独的环境;但是如果您希望这看起来像是您自己登录时的系统范围内的有效环境,则可以将激活节添加到您的
.profile
或类似内容中。替换
与
例如,安装到
/usr/local/bin
而不是/usr/bin
。并确保路径中的
/usr/local/bin
在/usr/bin
之前。即
通过添加
到
~/.bashrc
文件的末尾。通常建议使用自定义bin文件夹(如/usr/local/bin
或/opt/<some install>/bin
)作为PATH
环境变量的前缀,以确保在默认系统文件夹之前找到自定义。不知怎的,python 3回来了(在一些更新之后?)并导致了apt更新的大问题,所以我决定完全从备选方案中删除python 3:
相关问题 更多 >
编程相关推荐