Need to 'import gtk' on Ubuntu 20.04, what do I need?

Chris Green cl at isbd.net
Fri Jul 24 04:31:05 EDT 2020


Akkana Peck <akkana at shallowsky.com> wrote:
> Chris Green writes:
> > I have recently upgraded my desktop system from ubuntu 19.10 to ubuntu
> > 20.04.  [ ... ]
> > The error I am getting is:-
> [ ... ]
> >     ImportError: No module named gtk
> > 
> > So what do I need to install on my Ubuntu 20.04 system to provide the
> > gtk module?  
> 
> Ubuntu doesn't provide python-gtk any more at all. Which makes a
> number of programs rather difficult now ...
> 
Not quite true.  Originally this program failed with "ImportError: No
module named pygtk" so I fished around a bit (i.e. did some searches)
and installed python-gobject, python-gobject-2 and python-gi.  Doing
this got me past the "No module named pygtk", but I get the "No module
named gtk" still.  So Python GTK is sort of half available.


> > Alternatively (but much harder work) what is the Python 3 equivalent
> > of the the Python 2 pygtk and gtk modules.
> 
> ... because there is no close equivalent. In Python 3, GTK is
> accessed via something called GObject Introspection (module "gi")
> which requires significant changes to code beyond the usual 2to3
> Python migration.
> 
So is that what I'm getting by installing the things noted above.
I.e. 'import pygtk' is importing GObject things whereas 'import gtk'
is after the older stuff.

I'm a *fairly* competant Python programmer so, if I have to, I will
consider converting from using the gtk module to using the gi module,
are there any good tutorials which might help me down this road?


> You might be able to get the program working using pygtkcompat.
> Try inserting these lines near the beginning of the program:
> 
> from gi import pygtkcompat
> pygtkcompat.enable()
> pygtkcompat.enable_gtk(version='3.0')
> 
Ah, that sounds like it might be a reasonable path to try, I'll look
for documentation on pygtkcompat, it sounds as if that might be useful.


> If that doesn't work, you might be able to get the old Python 2/GTK 2
> packages working by installing these two files with dpkg -i.
> No warranty of safety or efficacy implied.
> 
> http://mirrors.kernel.org/ubuntu/pool/universe/p/pygtk/python-gtk2_2.24.0-6_amd64.deb
> http://mirrors.kernel.org/ubuntu/pool/universe/g/gimp/gimp-python_2.10.8-2_amd64.deb
> 
Yes, I found one thread on stackoverflow which went down this sort of
route but it's not going to be a long term solution I suspect.  I
think modernising and maintaining my own version of the code is going
to work better long term.


> Good luck! It was a nasty shock discovering that Ubuntu had removed
> python-gtk, especially since they kept lots of other python2 packages
> (I could have understood it if they'd removed python2 entirely).
> I don't know what the reasoning was for removing python-gtk while
> keeping python2.
> 
Yes, from my searches it seems to have bitten quite a few people.

Thanks for all the help.

-- 
Chris Green
·


More information about the Python-list mailing list