pygtk.require()

I notice that pygtk.require(‘2.0’) calls have appeared
in backend_gtk.py and backend_gdk.py

I am not convinced that library code should include calls to
pygtk.require()

The problem is that pygtk.require() has to be called >before<
importing gtk.

This means that if you are using gtk then you have two choices:

  1. never use pygtk.require()
  2. always precede any ‘import gtk’ with ‘import
    pygtk’ and ‘pygtk.require(‘2.0’)’
    I have found that 1. works pretty well and prefer not to litter
    code with the pygtk.require() boiler plate.

Unfortunately, approach 1. falls over if you use some library that
decides to use pygtk.require().

In the times when I do require a specific version of pygtk then I can
always create a simple wrapper that does the pygtk require stuff.

Now no doubt I’m missing some subtleties as to why it is good to have
these pygtk.require() calls in matplotlib. Is there any way to make
them optional?

A half-way house would be to do something like this:

try:

import pygtk

if not matplotlib.FROZEN and not sys.modules.has_key('gtk'):

    pygtk.require('2.0')

except:

print >> sys.stderr, sys.exc_info()[1]

raise SystemExit('PyGTK version %d.%d.%d or greater is required to

run ’

                 'the GTK Matplotlib backends'

                 % pygtk_version_required)

This code would ensure that the correct pygtk is loaded for people who
haven’t already import’ed gtk without throwing an exception for those
that have loaded gtk prior to importing matplotlib.

The down side with this fix is that it doesn’t help anyone who does
want the pygtk.require() but has made the mistake of importing gtk
first.

Any thoughts?

John