python - MacPorts 说当运行 "python --version"时我仍然有 Python 2.7

标签 python python-3.x macports

在指示 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 必须

  1. 遍历 $PATH 中的条目,并针对每个条目
  2. 发出一个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/

相关文章:

python-3.x - PysimpleGUI 中的下拉菜单

python-3.x - 类型错误 : Input 'b' of 'MatMul' Op has type float32 that does not match type int32 of argument 'a' - even after casting

macports - Macport 跳过依赖

python - Pandas - 检查一个数据框中的字符串列是否包含来自另一个数据框的一对字符串

python - 如何制作函数 Composer

python - 如果传递了特定输入并跳过代码末尾,则尝试立即重新启动方法

macos - macports 困惑

macports - OSX Mavericks ATLAS 从源和 Macports 安装失败

python - Scapy 将 proto 作为 ipv4 数据包的 ipv6 发送

python - 获取数据帧的协方差返回