在指示 MacPorts 切换到 Python 3.4 后,python --version
仍然输出 2.7.10。请注意,“which python”确实显示/opt/local/bin 在我的 PATH 中位于/usr/bin/之前:
$ which python
/opt/local/bin/python
$ python --version
Python 2.7.10
$ ls -l /opt/local/bin/python
lrwxr-xr-x 1 root wheel 24 Aug 1 10:00 /opt/local/bin/python -> /opt/local/bin/python2.7
$ sudo port select --list python
Available versions for python:
none
python26-apple
python27 (active)
python27-apple
python34
$ sudo port select --set python python34
Selecting 'python34' for 'python' succeeded. 'python34' is now active.
$ which python
/opt/local/bin/python
$ python --version
Python 2.7.10
$ ls -l /opt/local/bin/python
lrwxr-xr-x 1 root wheel 24 Aug 1 10:00 /opt/local/bin/python -> /opt/local/bin/python3.4
请注意符号链接(symbolic link)是如何改变的,但声明的版本没有改变。给了什么?
最佳答案
tl;dr:运行 hash -r
。
出于速度原因,当您在 shell 中键入 python
时,shell 会缓存需要运行的可执行文件。
考虑一下如果没有这样的缓存,shell 必须做什么:对于您输入的每个命令(不是绝对路径),shell 必须
- 遍历
$PATH
中的条目,并针对每个条目 - 发出一个
stat(2)
系统调用来测试该命令是否存在于当前搜索的目录中。请记住,在最初开发 shell 时,这可能涉及慢速旋转磁盘甚至网络文件系统。
为了加快速度,大多数 shell 只会为每个命令执行一次,直到 $PATH
被更改,或者您手动告诉 shell 删除缓存(例如使用 hash - bash 中的 r
,其他一些 shell 中的 rehash
。
不过,对我来说有些 shell 也缓存符号链接(symbolic link)是个新闻。
关于python - MacPorts 说当运行 "python --version"时我仍然有 Python 2.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45432353/