2017-08-01 58 views
3

在指示MacPorts切換到Python 3.4之後,python --version仍輸出2.7.10。請注意,「這蟒蛇」它只表示的/ opt/local/bin目錄之前來的/ usr/bin中/在我的道路:MacPorts表示,當運行「python --version」時,我仍然擁有Python 2.7

$ 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 

注意符號鏈接怎麼做改變,但所陳述的版本不會改變。是什麼賦予了?

+2

嘗試在符號鏈接更改後重新引導shell。 – Zcode

+0

是的!這工作。你能簡單解釋爲什麼shell每次都不重新讀取符號鏈接嗎? – Labrador

+0

我很抱歉,但我在GNU/Linux系統中是一個初學者,我知道當你創建一個別名時(例如在'bash_aliases'文件中),你必須刷新bash shell。 – Zcode

回答

3

tl; dr:運行hash -r


由於速度的原因,炮彈不斷的可執行什麼需要運行時鍵入python到外殼緩存。

考慮一下殼將有沒有這樣的緩存做:對於每一個命令(不是絕對路徑),您輸入,外殼必須

  1. 越過條目$PATH,並且對於每個條目
  2. 發出stat(2)系統調用以測試該命令是否存在於當前搜索的目錄中。請記住,當最初開發shell時,這可能涉及緩慢的旋轉磁盤或甚至網絡文件系統。

加快這,大部分炮彈將只爲每個命令這樣做一次,直到$PATH被改變,或者你手動告訴shell下降緩存(例如,使用在bash hash -rrehash在其他一些貝殼) 。

雖然,但有消息告訴我一些shell也會緩存符號鏈接。

+0

但是,符號鏈接沒有什麼特別之處:shell將二進制文件的路徑緩存起來,無論這個二進制文件是普通文件(即實際的二進制文件)還是符合真正的底層二進制文件的符號鏈接。 – mklement0

+0

好吧,'sudo port select --set python34'改變了'/ opt/local/bin/python'符號鏈接的目標,而shell將'python'映射到'/ opt/local/bin/python'。所以如果shell沒有在緩存中擴展這個符號鏈接,'python --version'應該返回3.4。但是它打印了2.7,所以bash必須在緩存中有符號鏈接'/ opt/local/bin/python - > python2.7'。 – neverpanic

+0

這是一個好點,謝謝,我沒有注意到這個矛盾。但是,我無法重新創建這個問題:''hash -l'會將緩存的路徑列爲符號鏈接(如果適用),甚至以下測試命令也會使符號鏈接的目標立即生效(在Bash v3.2.57和v4.3.42上進行了驗證,從包含在PATH中的文件夾運行):'ln -f -s $(which date)btmp; BTMP; ln -s -f $(which whoami)btmp; BTMP; rm btmp'。 – mklement0

相關問題