Search code examples
pythonpygobjectmsys2

Difference between MSYS2 python interpreter and standard python interpreter for PyGObject?


I installed PyGObject according to Gtk+ documentation using MSYS2 : https://www.gtk.org/download/windows.php

When I run following code :

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

It works fine using python3.7 from C:\msys64\mingw64\bin folder.

But when I try to run it using my standard python interpreter or an embedded interpreter adding msys2 site-packages to path I get an error

import sys
sys.path.append(r"C:\msys64\mingw64\lib\python3.7\site-packages")
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
ImportError: cannot import name '_gi' from 'gi' (C:\msys64\mingw64\lib\python3.7\site-packages\gi\__init__.py)

Why ? What's the difference between msys2 python interpreter and an other interpreter ? Is there a way to solve this ?

At the end I would like to make PyGObject available for an existing embedded python interpreter which live in a foreign software.


Solution

  • The version of Python that is installed in MSYS2 is compiled with GCC. When you install gobject-introspection, it was compiled with GCC and linked to the GTK libraries which were also compiled using GCC. In the mingw repository, Python is heavily patched to get it to run in this unique environment.

    Unfortunately, it won't be possible to run Python packages that are built in MSYS2 and run them using the regular Windows interpreter that is built with MSVC. You can package an app, using PyInstaller or other methods with the Mingw-w64 Python interpreter and run it in Windows. You will have to do your development using the Python interpreter in MSYS2.