Gtk4 + Python problems

I’m trying to create “the first hello world” app using gtk4 in python, but it doesn’t work, How can i fix? ERROR:

$ python3 main.py
Traceback (most recent call last):
  File "C:/msys64/home/danyf/main.py", line 3, in <module>
    import gi
ModuleNotFoundError: No module named 'gi'

I’m using MSY2 on Windows 10. ( i’ve installed the dependencies using pacman -S mingw-w64-i686-gtk4 mingw-w64-i686-python3-gobject )
main.py:

import sys

import gi

gi.require_version("Gtk", "4.0")
from gi.repository import GLib, Gtk


class MyApplication(Gtk.Application):
    def __init__(self):
        super().__init__(application_id="com.example.MyGtkApplication")
        GLib.set_application_name('My Gtk Application')

    def do_activate(self):
        window = Gtk.ApplicationWindow(application=self, title="Hello World")
        window.present()


app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)```

You’re asking for GTK 4, and yet you installed the GTK3 packages?

This means that the pygobject module isn’t found; you may need to change the environment, but it’s an MSYS2-specific question.

Copy-paste error, i’m using gtk4

How must i fix?

( i’m sorry for the errors but it’s my first post )

You installed the mingw-w64-i686 version, so make sure you call python3 main.py from the mingw32.exe terminal.

it doesn’t work, i’ve altredy tried

huh… then it’s weird…

Let me have a look.

Did the install of mingw-w64-i686-python3-gobject worked?

The correct package name seems to be mingw-w64-i686-python-gobject
(without “3”)

doesn’t work,
i’m actualy chanching techcnology ( msy2 → GitHub - wingtk/gvsbuild: GTK stack for Windows )

looks like python itself is not automatically installed as dependency…

Maybe try to install it too:
pacman -S mingw-w64-i686-python

I suspect it hasn’t found the python from msys2 and used another one that was in the PATH but doesn’t have the gi module.

doesn’t work, he give me this error:

$ py main.py
Traceback (most recent call last):
  File "C:\msys64\home\danyf\main.py", line 3, in <module>
    import gi
ModuleNotFoundError: No module named 'gi'

What’s the result of the commands where python and which python ?

i’m actually using the last version on python ( 3.12 )

OK, so that’s not the Python from msys2, so it can’t find gi for sure.

/mingw32/bin/python main.py should work now I think

Thank you, it work, how can i add the short cut:
python = /mingw32/bin/python??

Should be automatic if you (re)start the mingw32.exe console.

Just don’t call py, it’s not recognized and will point to some random windows install that isn’t compatible with msys2.

doesn’t work, is a good choice uninstall and reinstalling ONLY python?

error;

danyf@LAPTOP-0TRV5DSN MINGW64 /c/Users/danyf/Documents/Progetti/GTK GUI
$ python main.py
Traceback (most recent call last):
  File "C:/Users/danyf/Documents/Progetti/GTK GUI/main.py", line 3, in <module>
    import gi
ModuleNotFoundError: No module named 'gi'

From what I can see in your last message, you started mingw64.exe instead of mingw32.exe

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.