linux上有两个版本的python.如何使 2.7 成为默认值 [英] Two versions of python on linux. how to make 2.7 the default
问题描述
我的 linuxbox 上有两个版本的 python:
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
如何使 2.7 成为默认版本,以便当我输入 python
时,它会将我置于 2.7 中?
How can I make 2.7 be the default version so when I type python
it puts me in 2.7?
推荐答案
您可能实际上并不想更改默认的 Python.
You probably don't actually want to change your default Python.
您的发行版在 /usr/bin
中安装了一个标准系统 Python,并且可能有依赖于此的脚本存在,并由 #!/usr/bin/env python
.您可以通常在 2.7 中运行 Python 2.6 脚本,但您愿意冒险吗?
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
会破坏包管理器管理包的能力.并且更改 PATH
中目录的顺序将影响除 Python 之外的许多其他内容.(实际上,/usr/local/bin
位于 /usr/bin
之前更为常见,这可能是您真正想要的——但如果您拥有它反过来说,大概有一个很好的理由.)
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.)
但是当您键入 python
时,您不需要需要更改默认 Python 以让系统运行 2.7.
But you don't need to change your default Python to get the system to run 2.7 when you type python
.
首先可以设置一个shell别名:
First, you can set up a shell alias:
alias python=/usr/local/bin/python2.7
在提示符下输入,或者如果您希望更改持久化,则将其放入您的 ~/.bashrc
中,现在当 you 输入 python
它运行您选择的 2.7,但是当您系统上的某些程序尝试使用 /usr/bin/env python
运行脚本时,它运行标准的 2.6.
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.
或者,只需从您的 2.7(或针对不同项目的单独 venv)创建一个虚拟环境),并在 venv 内完成您的工作.
Alternatively, just create a virtual environment out of your 2.7 (or separate venvs for different projects), and do your work inside the venv.
这篇关于linux上有两个版本的python.如何使 2.7 成为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!