linux上有两个版本的python。如何使 2.7 成为默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19256127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:02:26  来源:igfitidea点击:

Two versions of python on linux. how to make 2.7 the default

pythonlinuxcentos

提问by Anthony

I've got two versions of python on my linuxbox:

我的 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

How can I make 2.7 be the default version so when I type pythonit puts me in 2.7?

我怎样才能让 2.7 成为默认版本,所以当我输入python它时,我会进入 2.7?

采纳答案by abarnert

You probably don't actually want to change your default Python.

您可能实际上并不想更改默认的 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 usuallyget away with running Python 2.6 scripts in 2.7, but do you want to risk it?

您的发行版在 中安装了标准系统 Python /usr/bin,并且可能具有依赖于此的脚本,并由#! /usr/bin/env python. 您通常可以在 2.7 中运行 Python 2.6 脚本,但您想冒险吗?

On top of that, monkeying with /usr/bincan break your package manager's ability to manage packages. And changing the order of directories in your PATHwill affect a lot of other things besides Python. (In fact, it's more common to have /usr/local/binahead 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.)

最重要的是,玩弄/usr/bin可能会破坏您的包管理器管理包的能力。并且更改目录的顺序PATH会影响除 Python 之外的许多其他内容。(事实上​​,更常见的是有/usr/local/bin提前/usr/bin,它可能是你真正想要的——但如果你有相反的方式,大概有一个很好的理由。)

But you don't needto change your default Python to get the system to run 2.7 when you type python.

但是,您无需更改默认 Python 即可在您键入python.



First, you can set up a shell alias:

首先,您可以设置一个shell别名:

alias python=/usr/local/bin/python2.7

Type that at a prompt, or put it in your ~/.bashrcif you want the change to be persistent, and now when youtype pythonit runs your chosen 2.7, but when some program on your system tries to run a script with /usr/bin/env pythonit runs the standard 2.6.

在提示符下输入,或者~/.bashrc如果您希望更改是持久的,则将其放入您的代码中,现在当输入python它时会运行您选择的 2.7,但是当您系统上的某些程序尝试使用/usr/bin/env python它运行脚本时,它会运行标准的 2.6。



Alternatively, just create a virtual environmentout of your 2.7 (or separate venvs for different projects), and do your work inside the venv.

或者,只需在 2.7 中创建一个虚拟环境(或为不同项目创建单独的 venv),然后在 venv 中进行工作。

回答by Amber

Add /usr/local/binto your PATHenvironment variable, earlier in the list than /usr/bin.

添加/usr/local/bin到您的PATH环境变量中,在列表中比/usr/bin.

Generally this is done in your shell's rc file, e.g. for bash, you'd put this in .bashrc:

通常这是在你的 shell 的 rc 文件中完成的,例如对于 bash,你可以把它放在.bashrc

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

This will cause your shell to look first for a pythonin /usr/local/bin, before it goes with the one in /usr/bin.

这将导致您的外壳首先查找pythonin /usr/local/bin,然后再查找in /usr/bin

(Of course, this means you also need to have /usr/local/bin/pythonpoint to python2.7- if it doesn't already, you'll need to symlink it.)

(当然,这意味着您还需要/usr/local/bin/python指向python2.7- 如果还没有,则需要对其进行符号链接。)

回答by emesik

I guess you have installed the 2.7 version manually, while 2.6 comes from a package?

我猜你已经手动安装了 2.7 版本,而 2.6 来自一个包?

The simple answer is: uninstall python package.

简单的答案是:卸载python包。

The more complex one is: do not install manually in /usr/local. Build a package with 2.7 version and then upgrade.

更复杂的一种是:不要在/usr/local 中手动安装。构建一个 2.7 版本的包,然后升级。

Package handling depends on what distribution you use.

包处理取决于您使用的发行版。

回答by Prakash Kamath

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.

所有操作系统都带有默认版本的 python,它驻留在 /usr/bin 中。操作系统附带的所有脚本(例如 yum)都指向此版本的 python 驻留在 /usr/bin 中。当您想安装新版本的 python 时,您不想破坏可能不适用于新版本 python 的现有脚本。

The right way of doing this is to install the python as an alternate version.

正确的做法是安装 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

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

现在通过这样做,像 yum 这样的现有脚本仍然可以与 /usr/bin/python 一起使用。您的默认 python 版本将是安装在 /usr/local/bin 中的版本。即当你输入 python 你会得到 2.7.3

This happens because. $PATH variable has /usr/local/bin before usr/bin.

发生这种情况是因为。$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

If python2.7 still does not take effect as the default python version you would need to do

如果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"

回答by Sreenath

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'

您的默认 python2.7 软链接到文本“python”

So remove the softlink python

所以删除软链接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

然后为python3.6新建一个软链接

ln -s /usr/bin/python3.6 python

ln -s /usr/bin/python3.6 python

Then try the command pythonin terminal

然后python在终端中尝试命令

//output:
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux

Type help, copyright, creditsor licensefor more information.

类型helpcopyrightcreditslicense了解更多信息。

回答by Shiv Buyya

Verify current version of python by:

通过以下方式验证 python 的当前版本:

$ python --version

then check python is symbolic link to which file.

然后检查python是哪个文件的符号链接。

  $ 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:

检查其他可用版本的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:

如果要将当前版本的 python 更改为 3.6 版本,请编辑文件 ~/.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

为 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

On Success, check current version of pip:

成功后,检查 pip 的当前版本:

$ pip3 -V

Output Ex:

输出示例:

pip 1.5.4 from /usr/lib/python3/dist-packages (python 3.6)