Wil*_*ill 17 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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
而且一切正常。大!:)
$ python -V
Python 3.5.2
Run Code Online (Sandbox Code Playgroud)
但是不久之后,我就意识到在安装和删除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)
Run Code Online (Sandbox Code Playgroud)
最终,我猜想它希望将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
Run Code Online (Sandbox Code Playgroud)
然后再次工作
$ 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) ...
Run Code Online (Sandbox Code Playgroud)
因此,我不得不将其保留为默认值python 2,但我使用python 3开发,因此希望我的系统在运行python和闲置时默认为python 3。
谁能告诉我如何做到这一点而又不至于跌倒?
我的系统是运行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
Run Code Online (Sandbox Code Playgroud)
(实际上是手臂v8)
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.2 LTS"
Run Code Online (Sandbox Code Playgroud)
Dai*_*ood 23
对于 2021 年或以后发现这个问题的人来说,几乎所有早期的答案都已经过时了。
指向 Python 3是完全可以的,也是现在所期望的/usr/bin/python。Python 2 没有通过安全修复程序进行更新,因此任何仍在使用它的系统都应该升级为使用 Python 3 作为系统 Python。任何现代发行版都应该已经解决了将系统 Python 升级到 Python 3 时潜在的不兼容性问题。
tri*_*eee 14
根据Debian策略,python指的是Python 2和python3Python3。请不要尝试在整个系统范围内进行更改,否则您将遇到已经发现的麻烦。
虚拟环境允许您使用任何版本的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
Run Code Online (Sandbox Code Playgroud)
无论如何,通常的做法是为您从事的每个项目都有一个单独的环境。但是,如果您希望这样看起来对您自己的登录有效,那么可以将激活节添加到您的登录名.profile或类似物中。
小智 5
更换
[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
Run Code Online (Sandbox Code Playgroud)
与
[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
Run Code Online (Sandbox Code Playgroud)
例如安装/usr/local/bin而不是/usr/bin。
并确保PATH中的/usr/local/bin之前/usr/bin。
即
[bash:~] $ echo $PATH
/usr/local/bin:/usr/bin:/bin
Run Code Online (Sandbox Code Playgroud)
通过添加来确保始终如此
export PATH=/usr/local/bin:$PATH
Run Code Online (Sandbox Code Playgroud)
到~/.bashrc文件末尾。通常建议PATH使用自定义bin文件夹为环境变量加上前缀,例如/usr/local/bin或,/opt/<some install>/bin以确保在默认系统设置之前找到自定义设置。
因为我不想破坏任何东西,所以我这样做是为了能够使用比 Python v3.4 更新的 Python3 版本:
$ 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*
Run Code Online (Sandbox Code Playgroud)
不知何故,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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36755 次 |
| 最近记录: |