问题:Linux上的两个版本的python。如何使2.7成为默认值
我的linuxbox上有两个版本的python:
$python
Python 2.6.6 (r266:84292, Jul 10 2013, 22:48:45)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ /usr/local/bin/python2.7
Python 2.7.3 (default, Oct 8 2013, 15:53:09)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ which python
/usr/bin/python
$ ls -al /usr/bin/python
-rwxr-xr-x. 2 root root 4864 Jul 10 22:49 /usr/bin/python
如何将2.7设置为默认版本,以便在键入python
时将其置于2.7版本?
I’ve got two versions of python on my linuxbox:
$python
Python 2.6.6 (r266:84292, Jul 10 2013, 22:48:45)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ /usr/local/bin/python2.7
Python 2.7.3 (default, Oct 8 2013, 15:53:09)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ which python
/usr/bin/python
$ ls -al /usr/bin/python
-rwxr-xr-x. 2 root root 4864 Jul 10 22:49 /usr/bin/python
How can I make 2.7 be the default version so when I type python
it puts me in 2.7?
回答 0
您可能实际上不想更改默认的Python。
您的发行版在中安装了标准系统Python /usr/bin
,并且可能具有依赖于此脚本的脚本,并由选择#! /usr/bin/env python
。通常,您可以在2.7中运行Python 2.6脚本,但是您要冒险吗?
最重要的是,闲逛/usr/bin
可能会破坏您的包管理器管理包的能力。并且更改目录中的顺序PATH
将影响除Python以外的许多其他因素。(实际上,在/usr/local/bin
之前更常见/usr/bin
,这可能是您真正想要的-但是如果您有其他选择,则可能有充分的理由。)
但是,您无需更改默认的Python即可在键入时使系统运行2.7 python
。
首先,您可以设置一个shell别名:
alias python=/usr/local/bin/python2.7
在提示符下键入该命令,或者~/.bashrc
如果您想使更改持久化,则将其放入您的计算机,现在键入该命令时,python
将运行您选择的2.7,但是当系统上的某个程序尝试使用/usr/bin/env python
标准2.6 运行该脚本时。
或者,只需在2.7(或针对不同项目的单独venv)中创建一个虚拟环境,然后在venv中进行工作。
You probably don’t actually want to change your default Python.
Your distro installed a standard system Python in /usr/bin
, and may have scripts that depend on this being present, and selected by #! /usr/bin/env python
. You can usually get away with running Python 2.6 scripts in 2.7, but do you want to risk it?
On top of that, monkeying with /usr/bin
can break your package manager’s ability to manage packages. And changing the order of directories in your PATH
will affect a lot of other things besides Python. (In fact, it’s more common to have /usr/local/bin
ahead of /usr/bin
, and it may be what you actually want—but if you have it the other way around, presumably there’s a good reason for that.)
But you don’t need to change your default Python to get the system to run 2.7 when you type python
.
First, you can set up a shell alias:
alias python=/usr/local/bin/python2.7
Type that at a prompt, or put it in your ~/.bashrc
if you want the change to be persistent, and now when you type python
it runs your chosen 2.7, but when some program on your system tries to run a script with /usr/bin/env python
it runs the standard 2.6.
Alternatively, just create a virtual environment out of your 2.7 (or separate venvs for different projects), and do your work inside the venv.
回答 1
添加/usr/local/bin
到您的PATH
环境变量中,在列表中早于/usr/bin
。
通常,这是在您外壳的rc文件中完成的,例如,对于bash,您可以将其放入.bashrc
:
export PATH="/usr/local/bin:$PATH"
这将导致你的shell先寻找一个python
在/usr/local/bin
,它会与一个之前/usr/bin
。
(当然,这意味着您还需要/usr/local/bin/python
指向python2.7
-如果尚未指向,则需要对其进行符号链接。)
Add /usr/local/bin
to your PATH
environment variable, earlier in the list than /usr/bin
.
Generally this is done in your shell’s rc file, e.g. for bash, you’d put this in .bashrc
:
export PATH="/usr/local/bin:$PATH"
This will cause your shell to look first for a python
in /usr/local/bin
, before it goes with the one in /usr/bin
.
(Of course, this means you also need to have /usr/local/bin/python
point to python2.7
– if it doesn’t already, you’ll need to symlink it.)
回答 2
通过以下方式验证python的当前版本:
$ python --version
然后检查python是指向哪个文件的符号链接。
$ ll /usr/bin/python
输出示例:
lrwxrwxrwx 1 root root 9 Jun 16 2014 /usr/bin/python -> python2.7*
检查其他可用的python版本:
$ ls /usr/bin/python*
输出示例:
/usr/bin/python /usr/bin/python2.7-config /usr/bin/python3.4 /usr/bin/python3.4m-config /usr/bin/python3.6m /usr/bin/python3m
/usr/bin/python2 /usr/bin/python2-config /usr/bin/python3.4-config /usr/bin/python3.6 /usr/bin/python3.6m-config /usr/bin/python3m-config
/usr/bin/python2.7 /usr/bin/python3 /usr/bin/python3.4m /usr/bin/python3.6-config /usr/bin/python3-config /usr/bin/python-config
如果要将python的当前版本更改为3.6版本,请编辑文件〜/ .bashrc:
vim ~/.bashrc
在文件末尾添加以下行并保存:
alias python=/usr/local/bin/python3.6
为python 3.6安装pip
$ sudo apt-get install python3.6 python3.6-dev
$ sudo curl https://bootstrap.pypa.io/ez_setup.py -o - | sudo python3.6
$ sudo easy_install pip
成功后,检查pip的当前版本:
$ pip3 -V
输出示例:
pip 1.5.4 from /usr/lib/python3/dist-packages (python 3.6)
Verify current version of python by:
$ python --version
then check python is symbolic link to which file.
$ ll /usr/bin/python
Output Ex:
lrwxrwxrwx 1 root root 9 Jun 16 2014 /usr/bin/python -> python2.7*
Check other available versions of python:
$ ls /usr/bin/python*
Output Ex:
/usr/bin/python /usr/bin/python2.7-config /usr/bin/python3.4 /usr/bin/python3.4m-config /usr/bin/python3.6m /usr/bin/python3m
/usr/bin/python2 /usr/bin/python2-config /usr/bin/python3.4-config /usr/bin/python3.6 /usr/bin/python3.6m-config /usr/bin/python3m-config
/usr/bin/python2.7 /usr/bin/python3 /usr/bin/python3.4m /usr/bin/python3.6-config /usr/bin/python3-config /usr/bin/python-config
If want to change current version of python to 3.6 version edit file ~/.bashrc:
vim ~/.bashrc
add below line in the end of file and save:
alias python=/usr/local/bin/python3.6
To install pip for python 3.6
$ sudo apt-get install python3.6 python3.6-dev
$ sudo curl https://bootstrap.pypa.io/ez_setup.py -o - | sudo python3.6
$ sudo easy_install pip
On Success, check current version of pip:
$ pip3 -V
Output Ex:
pip 1.5.4 from /usr/lib/python3/dist-packages (python 3.6)
回答 3
输入命令
which python
//output:
/usr/bin/python
cd /usr/bin
ls -l
在这里你可以看到类似的东西
lrwxrwxrwx 1 root root 9 Mar 7 17:04 python -> python2.7
您的默认python2.7被软链接到文本’python’
所以删除softlink python
sudo rm -r python
然后重试上面的命令
ls -l
您可以看到该软链接已删除
-rwxr-xr-x 1 root root 3670448 Nov 12 20:01 python2.7
然后为python3.6创建一个新的软链接
ln -s /usr/bin/python3.6 python
然后python
在终端中尝试命令
//output:
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
类型help
,copyright
,credits
或license
了解更多信息。
Enter the command
which python
//output:
/usr/bin/python
cd /usr/bin
ls -l
Here you can see something like this
lrwxrwxrwx 1 root root 9 Mar 7 17:04 python -> python2.7
your default python2.7 is soft linked to the text ‘python’
So remove the softlink python
sudo rm -r python
then retry the above command
ls -l
you can see the softlink is removed
-rwxr-xr-x 1 root root 3670448 Nov 12 20:01 python2.7
Then create a new softlink for python3.6
ln -s /usr/bin/python3.6 python
Then try the command python
in terminal
//output:
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type help
, copyright
, credits
or license
for more information.
回答 4
所有操作系统都带有python的默认版本,并且位于/ usr / bin中。操作系统随附的所有脚本(例如yum)都指向/ usr / bin中驻留的该版本的python。当您想安装新版本的python时,您不想破坏可能不适用于新版本python的现有脚本。
正确的方法是将python安装为替代版本。
e.g.
wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2
tar xf Python-2.7.3.tar.bz2
cd Python-2.7.3
./configure --prefix=/usr/local/
make && make altinstall
现在,通过执行此操作,现有的脚本(如yum)仍可与/ usr / bin / python一起使用。而您的默认python版本将是/ usr / local / bin中安装的版本。即当您键入python时,您将获得2.7.3
这是因为。$ PATH变量在usr / bin之前具有/ usr / local / bin。
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
如果python2.7仍然无法作为默认python版本生效,则需要执行此操作
export PATH="/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
All OS comes with a default version of python and it resides in /usr/bin. All scripts that come with the OS (e.g. yum) point this version of python residing in /usr/bin.
When you want to install a new version of python you do not want to break the existing scripts which may not work with new version of python.
The right way of doing this is to install the python as an alternate version.
e.g.
wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2
tar xf Python-2.7.3.tar.bz2
cd Python-2.7.3
./configure --prefix=/usr/local/
make && make altinstall
Now by doing this the existing scripts like yum still work with /usr/bin/python.
and your default python version would be the one installed in /usr/local/bin.
i.e. when you type python you would get 2.7.3
This happens because. $PATH variable has /usr/local/bin before usr/bin.
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
If python2.7 still does not take effect as the default python version you would need to do
export PATH="/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
回答 5
我想您手动安装了2.7版本,而2.6是从软件包中获得的?
简单的答案是:卸载python软件包。
比较复杂的是:不要在/ usr / local中手动安装。用2.7版本构建软件包,然后升级。
包处理取决于您使用的发行版。
I guess you have installed the 2.7 version manually, while 2.6 comes from a package?
The simple answer is: uninstall python package.
The more complex one is: do not install manually in /usr/local. Build a package with 2.7 version and then upgrade.
Package handling depends on what distribution you use.