How do I fix "'module' object has no attribute 'required_version'" for "gi.require_version('Gtk', '3.0')"?
The gi
module you are importing isn't the one from Ubuntu's GI package python-gi
but something you installed manually in /usr/local/
. It's either a very old version or something different that just happened to be called gi
, too.
Remove it and make sure the package python-gi
is installed.
Related videos on Youtube
Neil Boyd
Updated on September 18, 2022Comments
-
Neil Boyd over 1 year
I'm working with some people to build a fake news detecting program and have made some great progress but out of 5 members of the group only 1 person can run the Python program. I am running a clean install of Ubuntu 16.04 that is only a few hours old and the person who can run the program has Fedora 26. The error I get when trying to run the program is:
conesco3@Conesco3:~/Documents/Alternative-News-Checker$ sudo python main.py [sudo] password for conesco3: Traceback (most recent call last): File "main.py", line 6, in <module> gi.require_version('Gtk', '3.0') AttributeError: 'module' object has no attribute 'require_version'
I went and installed gtk+-3.0 with:
sudo apt install gtk+-3.0
But that doesn't help.
I also installed Glade, which the UI part of the program is made with, to try and fix it. But that did not make this error go away either.
The files can be found here if you want to try and recreate the problem, I am stumped so any help is appreciated and I have tried several other suggestions for fixes like installing
python-gobject
and they haven't fixed the problem.I can run a smaller-scale program and get the same error (screenshot). This program is sufficient to produce the problem:
import gi gi.require_version("Gtk", "3.0") from gi.repository import Gtk window = Gtk.Window(title="Hello World") window.show() window.connect("delete-event", Gtk.main_quit) Gtk.main()
Running that simplified program looks like this:
conesco3@Conesco3:~$ cd Desktop/ conesco3@Conesco3:~/Desktop$ python2 hello.py Traceback (most recent call last): File "hello.py", line 2, in <module> gi.require_version("Gtk", "3.0") AttributeError: 'module' object has no attribute 'require_version' conesco3@Conesco3:~/Desktop$
EDIT: After suggestions made by someone else I created a virtual environment and piped the requirements from the Fedora computer into a txt file and in the virtual environment install them again; this gives the same error.
The output of
python2 -c 'import gi; print(gi.__file__)'
is:/usr/local/lib/python2.7/dist-packages/gi/__init__.pyc
-
Christopher B. Adkins over 6 yearsWhat's the output of
python2 -c 'import gi; print(gi.__file__)'
? -
Neil Boyd over 6 years@FlorianDiesch This returns: /usr/local/lib/python2.7/dist-packages/gi/__init__.pyc
-
Christopher B. Adkins over 6 yearsThat's not Ubuntu's GI package but something you installed manually. It's either a very old version or something different that just happened to be called
gi
, too. Try to remove it and make sure the packagepython-gi
is installed. -
Neil Boyd over 6 yearsYou are a life saver, at a 2 day hackathon and not been able to run the program for the past few hours. Thanks a lot.
-