In bash, “which” gives an incorrect path - Python versions

十年热恋 提交于 2019-11-27 02:42:48

问题


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

~> 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

回答1:


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

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

The type builtin will 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 $PATH search for the executable.




回答2:


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

# 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 which and type still gave incorrect answers, I'd be very grateful, and will give them a check-mark!

Thanks for all your guidance!



来源:https://stackoverflow.com/questions/7746240/in-bash-which-gives-an-incorrect-path-python-versions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!