| FAQ Index - Search - Recent Changes - Everything - Add entry |
2.6. FAQ 2.4 sucks and doesn't work. Truthfuly, how do I require a specific version of PyGTK (or When do I need to use pygtk.require())?
It depends (doesn't that always suck?).
If you installed pygtk to a non-standard directory (i.e., not in your python installation's site-packages directory), you will need to manually set pythonpath accordingly, and take care of python versions youself (checking pygtk_version, for instance). If you installed it into a standard directory, read below.
We have now a concept of a `default pygtk version'. This is the version that you get if you do a simple 'import gtk' in Python. Precisely which is the default version varies:
- If you are using a dual-install version of pygtk (faq 2.5) it depends on the contents of a file called pygtk.pth, which is installed into the python site-packages directory. It's written there by the pygtk source tarball's `make install' target, so if you compile and install pygtk2 after pygtk0 the default will be pygtk2 and vice-versa.
- If you are using an older version of pygtk, you will need to check pygtk_version yourself, noting that you won't be able to select versions using the pygtk module.
To *make sure* you get the right version, you can use:
import pygtk
pygtk.require("1.2") # or 2.0 etc
Which I recommend for maximum cross-site functionality. HOWEVER, not all versions of pygtk offer the pygtk module (again, see faq 2.5). So to do the right thing, you need to wrap that in a try/except clause, and use a check for the previous versions. Something like the following should work if you want to make sure you have version 1.2:
# Make sure we have version 1.2. Swap for 2.0 as necessary.
def get_gtk():
print """You need to get version 0.6 of PyGTK for this to work. You
can get source code from http://ftp.gnome.org/pub/gimp/gtk/python/v1.2/ """
raise SystemExit
try:
import pygtk
pygtk.require("1.2")
except ImportError:
try:
import gtk
except ImportError:
get_pygtk()
if not hasattr(gtk, "GtkWindow"): # renamed in version 2.0
get_pygtk()
except AssertionError:
get_pygtk()
import gtk # we KNOW this is 1.2 at this point
[...]
[The whole pygtk.pth handling has shown itself to be something of a mess, unfortunately, and a separate gtk2 namespace might have saved us some trouble, IMHO. - Kiko]
