pygtk import gtk error

2019-04-01 08:08发布

问题:

I downloaded everything described as in pygtk for installation. Everything went fine until when I tried to type "import gtk", it threw an ImportError as follows:

from gtk import _gtk  
ImportError: DLL load failed: ...(something unreadable)

Then I re-install the pygtk-2.22.0 again, the same problem existed. So what to do please? Thanks in advance!

回答1:

The error you describe is usually caused by the python bindings (pygtk/pygobject/pycairo) being unable to load a dll it needs to function properly. Most of those errors are either caused by:

  • the GTK+ runtime not being on your PATH environment variable. This has long been the advice on how to get pygtk working on Windows. Please don't change your user or system PATH environment variable, it is no longer needed with the all-in-one installer.
  • multiple GTK+ runtime versions are on your PATH environment variable and the first (leftmost) one is not compatible with the pygtk/pygobject/pycairo versions you use. This is why adding the GTK+ runtime to your PATH environment variable is a bad idea: it is easy to mix up versions (sometimes GTK+ related installers add their bin directory to PATH on installation which contains an older or incomplete runtime).
  • a rare case where some software package installed libintl.dll and iconv.dll into %WINDIR%\system or %WINDIR%\sytem32

The most straightforward way to avoid the dll hell described above is to use the PyGTK All-in-one installer (http://download.gnome.org/binaries/win32/pygtk/2.22/). It contains both the Python bindings, the GTK+ runtime and even Glade and does no longer require you to change the PATH environment variable.

Small warning: if you decide to use the all-in-one installer, you'll have to uninstall the separate pygtk/pygobject/pycairo packages you've used before (or you'll be in a world of trouble...)



回答2:

read the source code, perhaps there is a need for a specific version of pygtk

edit the source code to work with your pygtk version



回答3:

I had this issue as well. You didn't mention for sure in your answer, so I'll suggest the obvious (well...the obvious to people that have used it a while, perhaps). Did you use the following three lines of code to import? You have to use these, in order, to import PyGTK.

I'm assuming your version here is 2.24 like mine. If not, change it to the version you have.

import pygtk
pygtk.require('2.24')
import gtk

That should suffice in importing gtk.



回答4:

Problem

As others have noted, don't put gtk in the path. I know, its tempting, and it works for XYZ, but it (to say the least) gets confusing. Dependency hell is bad enough on a platform like Linux which tries to make it easy for you.

For clarification, what the PyGTK All In One does for you is to install (what appears to be) a full gtk+ runtime directly in your python packages folder, so e.g. in C:\Python\Lib\site-packages\gtk-2.0\runtime

Solution

NOTE: %YOURPYTHONPATH% is an EXAMPLE variable which contains the path of your Python installation (e.g. C:\Python, or C:\Python27, or whatever it actually is). I suggest setting %PYTHON_DIR% or %PYTHON_PATH% if you want to use a variable to do this, as more programs are likely to use this.

To get your XYZ program requiring GTK to work, add %YOURPYTHONPATH%\Lib\site-packages\gtk-2.0\runtime\bin to the PATH when running your program; the correct versions of the DLLs it needs to link against are in that folder. All other GTK+ runtimes I had installed (GTK+/GTK2-Runetime) gave me errors.

Again; Do not attempt to set a user or system level variable (don't open up the dialog pictured below) as this will likely cause problems for you later unless you VERY sure you know what you are doing. If you are reading this, you most likely don't know as much as you think you do. Instead, alter the path in a cmd prompt, or use a batch/script file to set it up for you.



回答5:

After you install the pygtk.org package, install each of these in the following order:

  • pycairo-1.8.6.win32-py2.6.exe
  • pygobject-2.20.0.win32-py2.6.exe
  • pygtk-2.16.0+glade.win32-py2.6.exe
  • gtk+-bundle_2.16.6-20100912_win32.zip

For more information:

http://freetstar.com/windows7-pygtk-gtk/

I got the secusses according the link on my PC.