如何更改 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:31:36  来源:igfitidea点击:

How do I change where Bash looks for Python in Linux?

pythonlinuxbash

提问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/bindirectory. So

我刚刚将我的 ReadyNas 从 python 2.3.5 更新到 python 2.6.6。升级将新版本放在/usr/local/bin目录中。所以

  • /usr/local/bin/pythonis Python 2.6.6
  • /usr/bin/pythonis Python 2.3.5
  • /usr/local/bin/python是 Python 2.6.6
  • /usr/bin/python是 Python 2.3.5

When I type pythonat a bash prompt tries to run /usr/bin/pythonor 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 pythonthat it only looks in /usr/binfor python?

如何更改 bash 查找 python 的位置?bash 目前如何决定当我输入python它只查找/usr/binpython 时?

回答by bradley.ayers

Your PATHenvironment 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/binat the start of your PATHenvironment variable. Add the following to your ~/.bashrcfile:

你的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 setcommand in bash.

您可以通过set在 bash 中运行命令来查看当前设置。

Alternatively, you can simply rename /usr/bin/pythonto /usr/bin/python2.3and 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/pythonpointing to the appropiate version. Avoid replacing the actual binaries if possible.

如果您是管理员,最简洁的方法是使用符号链接/usr/bin/python指向合适的版本。如果可能,请避免替换实际的二进制文件。

If you're not, then add a binfolder somewhere you have access to, and prepend it to the $PATHenvironment variable. Then create a symlink to the desired version of the python interpreter.

如果不是,则在bin您有权访问的位置添加一个文件夹,并将其添加到$PATH环境变量中。然后创建指向所需版本的 python 解释器的符号链接。

Cheers!

干杯!