next up previous contents
Next: Buttons 與 Layout Up: PyGTK Previous: PyGTK 簡介與開始   Contents

PyGTK 除錯

  1. import 的 gtk modules 找不到,錯誤訊息如下:
    [dywang@dywhd2 zzz]$ python mypy1.py 
    7
    'import site' failed; use -v for traceback
    Traceback (most recent call last):
      File "mypy1.py", line 5, in <module>
        import gtk
    ImportError: No module named gtk
    
  2. 查詢安裝套件 pygtk2 已安裝。
    [root@dywhd2 ~]# rpm -qa | grep pygtk
    pygtk2-libglade-2.16.0-3.el6.x86_64
    pygtksourceview-2.8.0-1.el6.x86_64
    pygtk2-2.16.0-3.el6.x86_64
    
  3. 查詢 pygtk module 在目錄 /usr/lib64/python2.6/site-packages/ 下。
    [root@dywhd2 ~]# find /usr/lib64/python2.6 -name pygtk*
    /usr/lib64/python2.6/site-packages/pygtk.pyc
    /usr/lib64/python2.6/site-packages/pygtk.py
    /usr/lib64/python2.6/site-packages/pygtk.pth
    /usr/lib64/python2.6/site-packages/pygtk.pyo
    
  4. 執行 python 後進入互動式模式, import pygtk 模組不成功,顯示 sys.path 發覺/usr/lib64/python2.6/site-packages/ 目錄並不在其中。
    [dywang@dywhd2 zzz]$ python
    7
    'import site' failed; use -v for traceback
    Python 2.6.5 (r265:79063, Nov 12 2010, 00:52:45) 
    [GCC 4.4.4 20100525 (Red Hat 4.4.4-5)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pygtk; pygtk.require('2.0')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named pygtk
    >>> import sys
    >>> print sys.path
    ['', '', '/usr/local/google_appengine', 
    '/usr/local/google_appengine/lib/django_1.4', 
    '/usr/lib64/python26.zip', 
    '/usr/lib64/python2.6/', 
    '/usr/lib64/python2.6/plat-linux2', 
    '/usr/lib64/python2.6/lib-tk', 
    '/usr/lib64/python2.6/lib-old', 
    '/usr/lib64/python2.6/lib-dynload']
    
  5. 解決方式:編輯 /etc/bashrc,將 PYTHONPATH 變數增加 /usr/lib64/python2.6/site-packages/ 目錄,讓任何用戶登入後就生效。
    [root@dywhd2 ~]# vim /etc/bashrc 
    [root@dywhd2 ~]# tail -n1 /etc/bashrc 
    export PYTHONPATH="/usr/lib64/python2.6/site-packages:${PYTHONPATH}"
    
  6. 重新取得 bash shell 後,查詢 PYTHONPATH 變數已包含 /usr/lib64/python2.6/site-packages/ 目錄。
    [dywang@dywhd2 ~]$ echo $PYTHONPATH
    /usr/lib64/python2.6/site-packages::/usr/local/google_appengine:/usr/local/google_appengine/lib/django_1.4
    
  7. 再執行 python 進入互動式模式,import pygtk 或 import gtk 都成功取得 module,不再出現錯誤訊息。
    [dywang@dywhd2 ~]$ python
    Python 2.6.5 (r265:79063, Nov 12 2010, 00:52:45) 
    [GCC 4.4.4 20100525 (Red Hat 4.4.4-5)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pygtk
    >>> import gtk
    >>>
    



2017-06-14