星期五, 6月 16, 2017

Ubuntu trusty python2 與 python3 的 PYTHONPATH

python2 的 sys.path 是

['',
 '/usr/local/lib/python2.7/dist-packages/python_nghttp2-1.3.5.dev0-py2.7-linux-x86_64.egg',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/home/ellery/.local/lib/python2.7/site-packages',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.7',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']

python3 的 sys.path 是
['',
 '/usr/local/lib/python3.4/dist-packages/onedrive_d-1.1.0dev-py3.4.egg',
 '/usr/local/lib/python3.4/dist-packages/daemonocle-0.8-py3.4.egg',
 '/usr/local/lib/python3.4/dist-packages/Send2Trash-1.3.0-py3.4.egg',
 '/usr/local/lib/python3.4/dist-packages/certifi-2015.11.20.1-py3.4.egg',
 '/usr/local/lib/python3.4/dist-packages/psutil-2.1.1-py3.4-linux-x86_64.egg',
 '/usr/lib/python3/dist-packages',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-x86_64-linux-gnu',
 '/usr/lib/python3.4/lib-dynload',
 '/home/ellery/.local/lib/python3.4/site-packages',
 '/usr/local/lib/python3.4/dist-packages']

會發現兩邊的 $HOME/.local/lib/pythonX.Y/site-packages 順序是不一樣的,照理來說,應該是要以 $HOME/.local/lib/pythonX.Y/site-packages 為優先,這樣才有機會安裝最新版本的 package 而無需更動到系統的 package。
那該怎麼辦呢?這可以修改 /etc/python3.4/sitecustomize.py 來解決這問題。
在 /etc/python3.4/sitecustomize.py 的最後加入

import os
import sys

sys.path.insert(0,
        os.path.join(
            os.path.expanduser("~"),
            ".local",
            "lib",
            "python3.4",
            "site-packages"))
Python3 在執行前,會先執行這個檔案,這裡在 sys.path 串列最前面插入 $HOME/.local/lib/pythonX.Y/site-packages ,就能解決這問題了。
參考資料:

沒有留言: