Can not import modules from gi.repository - python

I can not import modules from gi.repository.
Specifically not Gtk and GObject.
I experienced this error both on Ubuntu 14.04 LTS and after reinstall
also on Linux Mint 17.
from gi.repository import Gtk, GObject
Results in the 'unresolved reference' warning for the respective
modules. Interestingly enough my Gtk GUI can be compiled and works
perfectly fine. Yet, GObject is entirely out of function.
I tried to work around with altering import statements such as:
from gi.repository.Gtk import*
Even hard coding the import path via:
sys.path.append('/usr/lib/python2.7/dist-packages/gi')
None of these approaches have solved this frustrating error so far.
I have not found any concluding help or basic info on this issue,
neither anywhere on the web nor in Linux forums or here on stackoverflow.
I am not sure whether this problem lies on the Python or the Linux side of things.
Can anybody suggest how to solve this issue?
What additional information do I need to provide eventually.
Thanks!

If on a Debian-based system such as Ubuntu, you probably need to install the gir1.2-gtk-3.0 package and other similarly named packages for other libraries. I don't know what the name of the package would be on Mint, but it's probably something similar.

On Wheezy (Debian 7.8) installing python3-gi fixed the problem for me.

I stumbled upon this issue with some old github python examples and it helped for me to add lines:
import gi
gi.require_version("Gtk", "3.0")
reference:
https://python-gtk-3-tutorial.readthedocs.io/en/latest/introduction.html

Related

PyGIWarning gi.require_version('Notify', '0.7')

I am using elementary OS and I want to make my own notifications using python 3. I know that a simple way of doing that is by import subprocess. However, reading around the web I found out that it's not the most appropriate way of doing it. Instead I should use from gi.repository import Notify but I get this error
__main__:1: PyGIWarning: Notify was imported without specifying a version first. Use gi.require_version('Notify', '0.7') before import to ensure that the right version gets loaded.
I have been trying to solve this and using the following code seems to work.
import gi
gi.require_version("Notify", "0.7")
from gi.repository import Notify
Can someone explain the why this error was produced in the first place and why this is the way to solve it (if it's the correct way)? Thank you in advance.
PS. I asked in the www.elementaryos.stackexchange.com but I didn't get any answer. Maybe they are not familiar with this.
You can try installing package pip install gi==0.0.7 or pip install pgi==0.0.7
OR
you can use pgi instead of gi.
It happens because of version mismatch issue. I have same problem and just tried on my system and it works.

ImportError: cannot import name 'tksupport'

I guess this is a dumb question, but I don't know how to solve my problem. I googled about it and also searched SO with no results.
I'm learning tkinter and twisted, and was going through a basic example, but cannot import tksupport.
I'm running ubuntu mate 16.04 (32bits), and python3.5.
I installed twisted using apt-get install python3-twisted, then when it didn't work I installed once more using pip3 install twisted.
Both this commands seemed to work just fine, and one of my imports (reactor) is working. This is my import
from twisted.internet import tksupport, reactor
Can anyone tell me what am I doing wrong?
Thanks
Not all modules in twisted have been ported to Python 3. tksupport has not been ported.
Only some parts of Twisted have been ported, and therefore only a subset of modules are installed under Python 3. You can see the full list of ported modules in twisted.python.dist3.
Twisted Python3 documentation
List of twisted modules currently in Python3

The PIL library import fails

Being a complete begginer in python, I decided to install the python interpreter 3.4.4, and also PyDev plugin for eclipse IDE. I am also using windows 10.
I have encountered a problem regarding certain imports, namely : from PIL import Image, ImageTk, which is apparently an unresolved import.
I have looked at certain questions which were similar to my situation, but most of them gave a solution like installing packaged from the linux apt-get. Here are some topics I visited :
Uninstalling and reinstalling Pillow and pip,
Installing pillow-imaging-tk on Ubuntu,
This topic which left me very confused,
This one seemed helpful, but is on Unix OS again
So please, could someone explain to me why I am seeing this error, and how could I correct it if I absolutely want to use Eclipse, Pydev, windows 10 and Python 3.
Found the solution, here's what I did:
Set the PYTHONPATH like it is shown in this article, make sure python.exe is accessible via cmd,
Via cmd, type pip install pillow. Alternatively, you can enter the same command from Windows+R,
(Not sure if relevant step) Via eclipse, Windows->Preferences->PyDev->PythonInterpreterremove your interpreter to re-add it,
Restart eclipse.
For Python import problems in PyDev, the project web site has a page on interpreter configuration that is a good place to start. I recently had a similar problem that I solved by adding a module to the forced builtins tab.

Gtk+ Version Error on Linux

I run a python application which uses a glade builder file for its GUI.
I recently migrated to a KDE desktop (Debian Wheezy). After installing the Gtk bindings with apt
(gir1.2-gtk-3.0) I tried to following error message when starting the Python
code:
gi._glib.GError: XXX.glade: required gtk+ version 3.10, current version is 3.4
How could I best solve this?
Do I need to downgrade Gtk from 3.4 to 3.1? And if yes how do I do that?
How do I find version 3.10 in the repositories?
Or is something wrongly configured? Maybe the Gtk version being fixed in the
glade file which I imported from a different system, whose Gtk version might
have been different (do not remember)?
Or any other suggestions?
I do not think source code is helpful in this matter, as the problem most likely is located outside of it, nonetheless some extract from it:
from gi.repository import Gtk, GObject, GLib
wTree = Gtk.Builder()
class Test_GUI:
def __init__(self):
self.builder = Gtk.Builder()
self.builder.add_from_file("XXX.glade")
self.window = self.builder.get_object("MainScreen")
self.window.show_all()
OK, the solution was to recompile the .glade builder file.
Somehow the version conflict was that the glade file was created on
a different system than it got used at.
Can you tell us wich program you try to use ? Can you Modify the source code to fit with your Gtk version ? Does it work ?
The upgrade from Gtk 3.4 to 3.10 is not possible (with aptitude or apt-get); Gtk 3.10 can't be found in the official repository of debian (correct me if i'm wrong), see this link
However you can install a later unstable version, see this link (It's not recommand too).

No module named xml.etree.ElementTree in GTK, python

I'm working with Python 2.7.6, Windows 8.1, in PyCharm 3.1.3.
trying to run something that already worked and get the error:
File "C:\something\sources\ParamsWin.py", line 6, in
import gtk ImportError: No module named gtk
Tried to download GTK through project settings, and got:
ImportError: No module named xml.etree.ElementTree
Tried to import element tree package and got the same error.
I've been googling quite a bit and there seems to be a problem with python and this element.
anyone, ideas?
TIA
You say you tried to download GTK?
I've had problems in the past (and the xml.etree... thing does look familiar) when I just installed GTK on its own. The best way of installing gtk in a Windows platform, in my humble opinion, is to use one of the the all-in-one downloaders from here: http://ftp.acc.umu.se/pub/GNOME/binaries/win32/pygtk/2.24/.
Cheers,
Simon

Categories

Resources