问题描述
我的 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
中,现在当 您 键入 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 设为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!