在 bash 中,“which”给出了错误的路径 - Python 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7746240/
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-09-18 00:57:15  来源:igfitidea点击:

In bash, "which" gives an incorrect path - Python versions

pythonmacosbashwhich

提问by Chris Cooper

Can anyone explain how python 2.6 could be getting run by default on my machine? It looks like pythonpoints to 2.7, so it seems like whichisn't giving me correct information.

谁能解释python 2.6如何在我的机器上默认运行?它看起来像 python指向 2.7,所以它似乎which没有给我正确的信息。

~> python --version
Python 2.6.5
~> which python
/opt/local/bin/python
~> /opt/local/bin/python --version
Python 2.7.2
~> ls -l /opt/local/bin/python
lrwxr-xr-x  1 root  admin  24 12 Oct 16:02 /opt/local/bin/python -> /opt/local/bin/python2.7

When I generate an error, I see what's really getting run. Why could this be?

当我生成错误时,我会看到真正运行的内容。为什么会这样?

~> python -error-making-argument
Unknown option: -e
usage:     /Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.

And how can I correct it?

我该如何纠正它?

----Edit:----

- - 编辑: - -

From suggestions in comments:

来自评论中的建议:

~> alias
alias cp='cp -i'
alias gcc='gcc -Wall'
~> type python
python is /opt/local/bin/python

回答by Adam Rosenfield

Bash uses an internal hash tableto optimize $PATHlookups. When you install a new program with the same name as an existing program (pythonin this case) earlier in your $PATH, Bash doesn't know about it and continues to use the old one. The whichexecutable does a full $PATHsearch and prints out the intended result.

Bash 使用内部哈希表来优化$PATH查找。当您python$PATH. 该which可执行文件做了全面$PATH的搜索和打印出想要的结果。

To fix this, run the command hash -d python. This will delete pythonfrom Bash's hash table and force it to do a full $PATHsearch the next time you invoke it. Alternatively, you can also run hash -rto clear out the hash table entirely.

要解决此问题,请运行命令hash -d python。这python将从 Bash 的哈希表中删除,并$PATH在您下次调用它时强制它进行完整搜索。或者,您也可以运行hash -r以完全清除哈希表。

The typebuiltinwill tell you how a given command will be interpreted. If it says that a command is hashed, that means that Bash is going to skip the $PATHsearch for the executable.

type内建将告诉你如何在给定命令将被解释。如果它说命令被散列,则意味着 Bash 将跳过$PATH对可执行文件的搜索。

回答by Chris Cooper

I just checked my .bash_profile, and it contained the following:

我刚刚检查了我的.bash_profile,它包含以下内容:

# Setting PATH for MacPython 2.6
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:/usr/local/git/bin:${PATH}"
export PATH

Commenting this out has fixed my problem.

对此进行评论已解决了我的问题。

If someone can tell me why whichand typestill gave incorrect answers, I'd be very grateful, and will give them a check-mark!

如果有人能告诉我,为什么whichtype还是给了不正确的答案,我会非常感激,并给他们一个复选标记!

Thanks for all your guidance!

谢谢大家的指导!