- Getting StartedВ¶
- gi.repository Windows
- 2 Answers 2
- How do I install python3-gi within virtualenv?
- 6 Answers 6
- How to import Gtk, gi.repository? Best GUI for table grids? Tkinter * vs ttk? [closed]
- Freezing a PyGI app #1966
- Comments
- cinchent commented Apr 30, 2016 •
- matysek commented May 9, 2016
- htgoebel commented May 9, 2016
- virtuald commented May 9, 2016 •
- cinchent commented May 14, 2016
- cinchent commented May 14, 2016
- ic3man5 commented Sep 6, 2016
- virtuald commented Sep 6, 2016
- fritjofbuettner commented Sep 6, 2016 •
- ic3man5 commented Sep 7, 2016
- ic3man5 commented Sep 10, 2016
- Averell7 commented Nov 5, 2016 •
Getting StartedВ¶
To get things started we will try to run a very simple GTK based GUI application using the PyGObject provided Python bindings. First create a small Python script called hello.py with the following content and save it somewhere:
Before we can run the example application we need to install PyGObject, GTK and their dependencies. Follow the instructions for your platform below.
After running the example application have a look at the “Python GTK 3 Tutorial” for more examples on how to create GTK apps and the “PyGObject API Reference” for API documentation for all supported libraries.
- Go to http://www.msys2.org/ and download the x86_64 installer
- Follow the instructions on the page for setting up the basic environment
- Run C:\msys64\mingw64.exe — a terminal window should pop up
- Execute pacman -Suy
- Execute pacman -S mingw-w64-x86_64-gtk3 mingw-w64-x86_64-python3 mingw-w64-x86_64-python3-gobject
- To test that GTK 3 is working you can run gtk3-demo
- Copy the hello.py script you created to C:\msys64\home\
- In the mingw32 terminal execute python3 hello.py — a window should appear.
- Go to https://brew.sh/ and install homebrew
- Open a terminal
- Execute brew install pygobject3 gtk+3
- Change the working directory to where your hello.py script can be found
- Run python3 hello.py
For more details on how to use a virtualenv with PyGObject, see the “ Creating a Development Environment ” page.
gi.repository Windows
I’m developing an app which has to be 100% compatible on windows and on linux. On linux I have no problems, but on windows I came up with this message: from gi.repository import Gtk ImportError: No module named gi
I installed pygobject, pygtkallinone, gtk.. what am I missing?
2 Answers 2
Most of Havok answer is correct, except that documentation has been improved a lot lately, with a Tutorial:
Including a PDF version for offline reading:
And a complete API reference manual, not only of Gtk but also many other libraries that you can use with introspection:
on windows the support is great, a lot of effort has been put in creating all the windows executables and libraries, the last release of pygi-aio is 3.10.6:
I’ve also put instructions on the Gnome wiki on how to build on win32 with pygobject introspection, it’s not that hard, I’ve also uploaded a setup.py that actually works:
So the answer is, don’t go with PyGTK
gi.repository module is called PyGObject and is for Gtk+3 and is not yet available for Windows (there has been experiments, but is not ready AFAIK). https://live.gnome.org/PyGObject
gtk module is called PyGtk and is for Gtk+2 and is very mature on Windows platforms (in particular 2.24). http://www.pygtk.org/
What’s the difference?
- PyGObject has some scarse Windows support. PyGtk has great support for this OS.
- PyGObject is what is called a dynamic binding and PyGtk is a static one.
- PyGObject is new and the future and PyGtk is the past, mature and some day will be deprecated.
- PyGObject documentation and examples are very very limited, and PyGtk one are very very complete: http://developer.gnome.org/pygtk/stable/
- They are not compatible, you can’t use both and you need to choose one.
- PyGObject is in active development, no new features will be introduced for PyGtk.
- If you use Glade, the lastest version will be for PyGObject (for example 3.12.1). If you want to use Glade for PyGtk install the 3.8.0 version.
If you choose to go PyGtk there is great support for freezing the application. You can check my documentation and support files for my application here: http://sourceforge.net/p/nestededitor/code/247/tree/trunk/dist/windows/
EDIT:
Many things have changed in two years. Please see gianmt answer.
How do I install python3-gi within virtualenv?
I’m following the Python GTK+ 3 Tutorial and I’m trying to get a working install running in virtualenv. I have python3-gi installed through the Ubuntu package manager already. Things look like this:
As you can see, python3-gi is obviously not available within virtualenv but I am not sure how to install it since python3-gi is installed through my package manager and not with pip.
6 Answers 6
It is now possible to resolve this using vext. Vext allows you to install packages in a virtualenv that individually access your system packages. To access gi , do the following:
Update 2018 – Debian Stretch
Install GTK+ 3 / GIR.
Create a virtual environment.
Install pygobject ( pycairo should come as a dependency).
Update 2018 – macOS
Install GTK+ 3 and Gobject Introspection with Homebrew.
Create and activate a virtual environment.
Install pygobject ( pycairo should come as a dependency).
Original answer
This is what I did to get GTK+ 3 within a Python 3.5 virtual environment on OS X 10.11.
Install GTK+ 3 with Homebrew.
Create and activate a virtual environment.
Install pycairo on the virtual environment.
Install pygobject on the virtual environment.
Python 3.5 downloaded and installed from PSF.
I haven’t found a proper solution to this. When I run into situations where I can’t get something to install into a virtualenv directly, I symlink it there and it works fine (there are probably exceptions, but this is not one of them).
Not elegant in the slightest; seems nicer than giving the virtualenv full access to all system packages though (via —system-site-packages ).
The pip package name is somewhat counterintuitive — use pip install PyGObject .
I installed pgi via pip , which may be an option. It is apparently API compatibly with PyGObject and so far seems to work ok running Gtk .
On Ubuntu (tested in 19.04), you can download the package and then install it as follows:
In Ubuntu, the name of the downloaded package is python3-gi_3.32.0-1_amd64.deb .
How to import Gtk, gi.repository? Best GUI for table grids? Tkinter * vs ttk? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago .
I found a program, that uses gi, gi.repository, and Gtk. It seems like a great GUI for my Python script that involves Excel sheet imports and table grids.
I am finally able to import gi after much work, but still not able to import gi.repository or Gtk (No module named «») even though I believe I have them installed correctly.
I tried installing all the Gnome stuff and used the Jhbuild for GTK3 which only screwed all my files up. I uninstalled and reinstalled this stuff like 5 times. I also had to delete and reinstall Python.
I gave up on Jhbuild. Isn’t there a simpler way to install GTK? Or is there a better GUI?
I have been using tkinter in Python 3, but the only problem is that a script I found that incorporates a table grid uses ttk while I do not use ttk. I do not understand ttk and excessively definitive (i.e. tkinter.ttk.Label, etc vs tkinter *’s Label). Ttk seems obsolete; please correct me if I am wrong.
For my simple beginner scripts I have been using from tkinter import * , which one would think that importing all (*), would include ttk, but I guess it must be specified. Using ttk messes up everything in my script even when I translate everything from tkinter * to ttk. I am clearly doing something wrong or ttk is obsolete. I receive errors such as unexpected arguments, things are not defined, name errors, __init__ errors, etc. It seems you cannot mesh (newer?) tkinter * modules with (older?) ttk modules.
Again, if anyone knows the best, newest and simplest GUI, I would really appreciate it. I using Python 3 on a Mac (Sierra 10.12.3).
Freezing a PyGI app #1966
Comments
cinchent commented Apr 30, 2016 •
I was having no success freezing my PyGI (a.k.a. PyGObject) application into an executable. (Per the «Supported Packages», I was using the 3.2 preliminary version of PyInstaller, PyGi 3.18.2, Windows 7, Python 2.7.10)
To simplify the problem from my (somewhat large) PyGI app and try a more basic test, I picked a random PyGI example program ( spinner.py ) verbatim from the Gnome site. It executed fine as a script, but after freezing it as a single executable via pyinstaller -F spinner.py , this was the result when I launched the executable:
I won’t bore you with the details of what all I had to try to get past this problem, but the solution may be of interest to others.
It turns out that the PyGI installation may not include .gir files, which are necessary in the freezing process, in order for the PyGI introspection layer to have the correct API definitions for the Gnome libraries. (GIR files are XML representations of the type library files representing the APIs in binary form.)
On my system, the .typelib files were in C:\Python27\Lib\site-packages\gnome\lib\girepository-1.0 . The PyInstaller hooks expect to find them the corresponding .gir files in the C:\Python27\share\gir-1.0 directory, which I had to create. Using the g-ir-generate utility, which may be included in the PyGI installation bundle, it was a simple matter of executing:
g-i-generate Gtk-3.0.typelib >C:\Python27\share\gir-1.0\Gtk-3.0.gir
and so on, for each .typelib file.
The text was updated successfully, but these errors were encountered:
matysek commented May 9, 2016
@htgoebel and @codewarrior0 do you think that this is something that could be automated with the pygi hook or is this a candidate for a recipe?
htgoebel commented May 9, 2016
In PyInstaller.utils.hooks.gi.gir_library_path_fix we aleady call g-ir-compiler . So maybe this can be automated, too.
virtuald commented May 9, 2016 •
- How did you install PyGI? Via the all-in-one installer?
- Are there any gir files anywhere? Perhaps we’re just looking in the wrong place. I don’t think PyGI can function without the compiled files (or maybe it was the other way around, I forget)
cinchent commented May 14, 2016
Yes, I used the all-in-one installer.
No, there were no .gir files anywhere. After contacting the maintainer of the all-in-one installer (tumagonx), he acknowledged the lack of them, but (inexplicably enough) in the latest version (pygi-aio3.18.2-rev7) actually removed the g-ir-generate precompiled executable from the all-in-one bundle, even when the «Install Gir» option is checked! When I questioned the rationale for that, he said anyone could always download that project and build the executables from source, and omitted them because «who would need to build .gir files»?
cinchent commented May 14, 2016
Incidentally, there was a follow-up issue (#1968) related to this. In the end, there was a happy outcome to that, but there is an ongoing issue with the PyGObject hooks when a particular PyGObject module is imported, with certain dependent certain type libraries remaining absent. See that issue for details.
ic3man5 commented Sep 6, 2016
I get this same issue with Python3.4 but I don’t have a share directory.
looks like all my *.typelib files are in:
C:\Python34\Lib\site-packages\gnome\lib\girepository-1.0\
virtuald commented Sep 6, 2016
I’ll probably be playing with packaging my app once again later this month, so I can look at this then. I use the all-in-one installer, so I’m sure I’ll run into the same issues.
fritjofbuettner commented Sep 6, 2016 •
I am currently using this workaround to manually add the required typelibs in my .spec file:
hope this helps
Python 2.7.12 (anaconda)
PyGI 3.18 (AiO)
PyInstaller 3.3 (installed from Github)
ic3man5 commented Sep 7, 2016
Thank you, I’ll try this out and report back.
ic3man5 commented Sep 10, 2016
Can confirm this works:
here is the .spec file:
And for completeness here is the python file:
Averell7 commented Nov 5, 2016 •
Python 3.4
Pygi aio 3.18
Windows 7
The spec file of ic3man5 works for the test file he gives. I just think it is an overkill because it copies all the typelibs files in the dist directory, and this increases the siez of the files to distribute.
I suggest adding to the documentation the following procedure to find what pyinstaller has missed in the dependencies. I use this test file :
Notice the «require» lines are commented. The reason is that if they are present, the program will stop there with the error : Namespace Gtk not available and this does not give any information.
- Once pyinstaller has made its job with only 3 warnings, run the program. First come the errors about missing typelibs :
- gi.RepositoryError: Typelib file for namespace ‘GdkPixbuf’, version ‘2.0’ not found
- copy the typelib required from . \site-packages\gnome\lib\girepository-1.0\ to gi_typelibs
- run the program again
- new error : Typelib file for namespace ‘GModule’, version ‘2.0’ not found
- copy the typelibs file
- run the program, error : ImportError: cannot import name Poppler, introspection typelib not found
- copy the poppler typelibs
- run, new error : GLib.Error: g-invoke-error-quark: Could not locate poppler_document_new_from_file: ‘libpoppler-glib-8.dll’. Unable to find the file . This indicates a missing dll, which must be copied from . \gnome\ to the directory of the program.
- run the program. Now a window appear, indicating another missing dll : liblcms2-2.dll. Copy it.
- running the program in the same way will indicate that two others dll’s are missing : libopenjp2.dll and libstdc++.dll
- and now, at last, the program runs !
The whole procedure takes 10 minutes. Since the missing files are now known it is possible to include them in the spec file. I use the following code :
And in the analysis : binaries=missing_files