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

时间:2022-04-18 05:33:25

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 python it puts me in 2.7?

我怎么能把2.7变成默认的版本,所以当我输入python的时候,它把我变成了2.7?

3 个解决方案

#1


131  

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 usually get away with running Python 2.6 scripts in 2.7, but do you want to risk it?

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

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.)

最重要的是,使用/usr/bin可以破坏包管理器管理包的能力。改变路径中的目录顺序将会影响除Python之外的许多其他东西。(事实上,在/usr/bin前面有/usr/local/bin更常见,这可能是你想要的——但如果你有相反的方法,大概有一个很好的理由。)

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

但是,当您输入Python时,您不需要更改您的默认Python来让系统运行2.7。


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 ~/.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.

在提示符下输入,或者将其放入~/。如果您希望更改是持久的,那么现在当您输入python时,它运行您所选择的2.7,但是当您的系统上的某个程序尝试运行一个带有/usr/bin/env python的脚本时,它运行的是标准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.

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

#2


12  

Add /usr/local/bin to your PATH environment 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 python in /usr/local/bin, before it goes with the one in /usr/bin.

这将使您的shell首先在/usr/local/bin中查找python,然后再与/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.)

(当然,这意味着您还需要将/usr/local/bin/python指向python2.7——如果它还没有,那么您需要将其符号链接起来。)

#3


-7  

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.

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

#1


131  

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 usually get away with running Python 2.6 scripts in 2.7, but do you want to risk it?

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

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.)

最重要的是,使用/usr/bin可以破坏包管理器管理包的能力。改变路径中的目录顺序将会影响除Python之外的许多其他东西。(事实上,在/usr/bin前面有/usr/local/bin更常见,这可能是你想要的——但如果你有相反的方法,大概有一个很好的理由。)

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

但是,当您输入Python时,您不需要更改您的默认Python来让系统运行2.7。


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 ~/.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.

在提示符下输入,或者将其放入~/。如果您希望更改是持久的,那么现在当您输入python时,它运行您所选择的2.7,但是当您的系统上的某个程序尝试运行一个带有/usr/bin/env python的脚本时,它运行的是标准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.

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

#2


12  

Add /usr/local/bin to your PATH environment 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 python in /usr/local/bin, before it goes with the one in /usr/bin.

这将使您的shell首先在/usr/local/bin中查找python,然后再与/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.)

(当然,这意味着您还需要将/usr/local/bin/python指向python2.7——如果它还没有,那么您需要将其符号链接起来。)

#3


-7  

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.

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