Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MacPorts说我仍然有Python2.7当;python——版本“;正在运行_Python_Python 3.x_Macports - Fatal编程技术网

MacPorts说我仍然有Python2.7当;python——版本“;正在运行

MacPorts说我仍然有Python2.7当;python——版本“;正在运行,python,python-3.x,macports,Python,Python 3.x,Macports,指示MacPorts切换到Python 3.4后,Python--version仍然输出2.7.10。请注意,“哪条python”确实显示/opt/local/bin在我的路径中位于/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 /op

指示MacPorts切换到Python 3.4后,
Python--version
仍然输出2.7.10。请注意,“哪条python”确实显示/opt/local/bin在我的路径中位于/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

请注意符号链接是如何更改的,但声明的版本不会更改。给了什么?

tl;dr:Run
hash-r


出于速度方面的原因,当您在shell中键入
python
时,shell保留了需要运行的可执行文件的缓存

考虑一下,如果没有这样的缓存,shell将不得不做什么:对于您输入的每个命令(不是绝对路径),shell必须

  • 检查
    $PATH
    中的条目,并检查每个条目
  • 发出一个
    stat(2)
    系统调用,以测试当前搜索的目录中是否存在该命令。请记住,这可能涉及旋转速度较慢的磁盘,甚至是最初开发Shell时的网络文件系统
  • 为了加快速度,大多数shell只会对每个命令执行一次此操作,直到更改
    $PATH
    ,或者手动告诉shell删除缓存(例如,在bash中使用
    hash-r
    ,在其他一些shell中使用
    rehash


    不过,有些shell也缓存符号链接,这对我来说是个新闻。

    在符号链接更改后尝试重新启动shell。是的!这起作用了。你能简单解释一下为什么shell每次都不重新读取符号链接吗?很抱歉,我是GNU/Linux系统的初学者,我只知道当你创建一个别名时(例如在
    bash\u alias
    文件中),你必须刷新bash shell。符号链接没有什么特别之处,尽管如此:shell会缓存二进制文件的路径,不管该二进制文件是常规文件(即实际二进制文件)还是指向真正的底层二进制文件的符号链接,shell将
    python
    映射到
    /opt/local/bin/python
    。因此,如果shell没有扩展缓存中的符号链接,
    python--version
    应该返回3.4。但是它确实打印了2.7,所以bash的缓存中一定有符号链接
    /opt/local/bin/python->python2.7
    。这是一个很好的观点,谢谢,我没有注意到矛盾。但是,我无法重新创建该问题:
    hash-l
    将缓存的路径列为符号链接(如果适用),甚至下面的测试命令也会使更改符号链接的目标立即生效(在Bash v3.2.57和v4.3.42上验证,从路径中包含的文件夹运行):
    ln-f-s$(哪个日期)btmp;btmp;ln-s-f$(哪个whoami)btmp;btmp;rm btmp