如何更改 Bash 在 Linux 中查找 Python 的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5546141/
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
How do I change where Bash looks for Python in Linux?
提问by James Scherer
I just updated my ReadyNas from python 2.3.5 to python 2.6.6. The upgrade placed the new version in the /usr/local/bin
directory. So
我刚刚将我的 ReadyNas 从 python 2.3.5 更新到 python 2.6.6。升级将新版本放在/usr/local/bin
目录中。所以
/usr/local/bin/python
is Python 2.6.6/usr/bin/python
is Python 2.3.5
/usr/local/bin/python
是 Python 2.6.6/usr/bin/python
是 Python 2.3.5
When I type python
at a bash prompt tries to run /usr/bin/python
or my old version. I relocated my old version, and now I get:
当我python
在 bash 提示符下输入时尝试运行/usr/bin/python
或我的旧版本。我重新定位了旧版本,现在我得到:
bash: /usr/bin/python: No such file or directory
How can I change where bash looks for python? How is bash currently deciding that when I type python
that it only looks in /usr/bin
for python?
如何更改 bash 查找 python 的位置?bash 目前如何决定当我输入python
它只查找/usr/bin
python 时?
回答by bradley.ayers
Your PATH
environment variable. It has a list of directories which bash searches (in the same order) when it's looking for an program to execute. Basically you want to put /usr/local/bin
at the start of your PATH
environment variable. Add the following to your ~/.bashrc
file:
你的PATH
环境变量。它有一个目录列表,bash 在寻找要执行的程序时搜索这些目录(以相同的顺序)。基本上你想把/usr/local/bin
你的PATH
环境变量放在开头。将以下内容添加到您的~/.bashrc
文件中:
export PATH=/usr/local/bin:$PATH
You can have a look at the current setting by running the set
command in bash.
您可以通过set
在 bash 中运行命令来查看当前设置。
Alternatively, you can simply rename /usr/bin/python
to /usr/bin/python2.3
and create a symlink pointing to the new version, e.g.
或者,您可以简单地重命名/usr/bin/python
为/usr/bin/python2.3
并创建一个指向新版本的符号链接,例如
ln -s /usr/local/bin/python /usr/bin/python
回答by slezica
I don't think it's BASH responsibility to choose the default version for the Python interpreter.
我认为为 Python 解释器选择默认版本不是 BASH 的责任。
If you're the administrator, the cleanest way to do this is to use a symbolic link in /usr/bin/python
pointing to the appropiate version. Avoid replacing the actual binaries if possible.
如果您是管理员,最简洁的方法是使用符号链接/usr/bin/python
指向合适的版本。如果可能,请避免替换实际的二进制文件。
If you're not, then add a bin
folder somewhere you have access to, and prepend it to the $PATH
environment variable. Then create a symlink to the desired version of the python interpreter.
如果不是,则在bin
您有权访问的位置添加一个文件夹,并将其添加到$PATH
环境变量中。然后创建指向所需版本的 python 解释器的符号链接。
Cheers!
干杯!