GTK3 with Python: invalid cast error in pygobject3

I’m trying to walk through a tutorial on GTK3 in Python and got stuck on the first code sample. The code is very simple:

#!/usr/bin/env nix-shell
#!nix-shell -i python -p python3 gtk3 gobject-introspection python3Packages.pygobject3

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

but it generates a mysterious error:

/nix/store/1p9m2ydf0126n28apjjr8nfww87gpjqm-python3.9-pygobject-3.42.0/lib/python3.9/site-packages/gi/overrides/Gdk.py:438: Warning: invalid cast from 'GIOModule' to 'GTypeModule'
  initialized, argv = Gdk.init_check(sys.argv)
/nix/store/1p9m2ydf0126n28apjjr8nfww87gpjqm-python3.9-pygobject-3.42.0/lib/python3.9/site-packages/gi/overrides/Gdk.py:438: Warning: g_type_module_use: assertion 'G_IS_TYPE_MODULE (module)' failed
  initialized, argv = Gdk.init_check(sys.argv)
**
dconf:ERROR:../gsettings/dconfsettingsbackend.c:215:g_io_module_unload: code should not be reached
Bail out! dconf:ERROR:../gsettings/dconfsettingsbackend.c:215:g_io_module_unload: code should not be reached

Could not google it, so I suspect that something is wrong with dependencies. What could be done to fix the problem? I’d prefer to run the script via nix-shell shebang.

That sounds like an issue with some environment contaminant. Try running with env GI_TYPELIB_PATH= GIO_EXTRA_MODULES= GDK_PIXBUF_MODULE_FILE= or adding --pure to the nix-shell args for starters.

1 Like

Thank you very much, using pure shell helped! Could you also give some advice on how to deal with such situations, especially when running inside Gnome? I have very little experience with developing for GTK unfortunately. For the record, I have the following variables set:

% env | grep -E 'GI_TYPELIB_PATH|GIO_EXTRA_MODULES|GDK_PIXBUF_MODULE_FILE'
GDK_PIXBUF_MODULE_FILE=/nix/store/2lrh5prpfws16jnjvr3jsmx3rx5jkrcp-librsvg-2.52.4/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache
GIO_EXTRA_MODULES=/nix/store/nsd0yyfd64dnzhlicbsg7xjfll65vcz9-dconf-0.40.0-lib/lib/gio/modules:/nix/store/nsd0yyfd64dnzhlicbsg7xjfll65vcz9-dconf-0.40.0-lib/lib/gio/modules:/nix/store/ci23fym5qf06k0vxa7iw9ilriddbiqd7-glib-networking-2.70.0/lib/gio/modules:/nix/store/nsd0yyfd64dnzhlicbsg7xjfll65vcz9-dconf-0.40.0-lib/lib/gio/modules:/nix/store/ci23fym5qf06k0vxa7iw9ilriddbiqd7-glib-networking-2.70.0/lib/gio/modules:/nix/store/nsd0yyfd64dnzhlicbsg7xjfll65vcz9-dconf-0.40.0-lib/lib/gio/modules:/nix/store/ci23fym5qf06k0vxa7iw9ilriddbiqd7-glib-networking-2.70.0/lib/gio/modules:/nix/store/wlvjymm8l7968f9f43lidl7m8gkqsx2x-gvfs-1.48.1/lib/gio/modules:/nix/store/nsd0yyfd64dnzhlicbsg7xjfll65vcz9-dconf-0.40.0-lib/lib/gio/modules
GI_TYPELIB_PATH=/nix/store/2ark77rn9g1lbyqv6p2jxpl4wx63ks4y-gtk+3-3.24.30/lib/girepository-1.0:/nix/store/2lrh5prpfws16jnjvr3jsmx3rx5jkrcp-librsvg-2.52.4/lib/girepository-1.0:/nix/store/ijmd647b4a120s7j3hx9q0p7ad6k9619-gsettings-desktop-schemas-41.0/lib/girepository-1.0:/nix/store/jrr2myh9h29fz9sbclr05sikiil1h8sw-atk-2.36.0/lib/girepository-1.0:/nix/store/w9flqvz7sjfmnzf8zxbshnrph52l15w6-gdk-pixbuf-2.42.6/lib/girepository-1.0:/nix/store/6jm1gz0pva1bjq2vfhxiz2zsd1qgilrw-harfbuzz-3.0.0/lib/girepository-1.0:/nix/store/rbhjha51mxyg87293d8ibn6nyw1mzwni-pango-1.48.10/lib/girepository-1.0:/nix/store/z8f6br212ywb3y5mqakvhr323am868ag-gcr-3.41.0/lib/girepository-1.0:/nix/store/bn739rdrvgnlpvgxkhqf4wvp2n4xv0yz-accountsservice-0.6.55/lib/girepository-1.0:/nix/store/s73n5xpadmi93d49l7v768djfxpgiclb-libsecret-0.20.4/lib/girepository-1.0:/nix/store/jszj7c5dzsvgsdakrlppkai8lyrv9nkn-polkit-0.120/lib/girepository-1.0:/nix/store/rp2p20m57mqqnx7jsbi7b5yv901clx9f-networkmanager-1.32.12/lib/girepository-1.0:/nix/store/09dnfcpyjl1rhigmcyl4ry4615ibg3kb-json-glib-1.6.6/lib/girepository-1.0:/nix/store/agfpkx8ac54vidxrmnq3cyx219clk88n-graphene-1.10.6/lib/girepository-1.0:/nix/store/n2aih1msh2n88mgb7g193bd7l9ni9wi2-libical-3.0.10/lib/girepository-1.0:/nix/store/5b60787z2d14zr9pbvwzrjrljm2fa7nn-libsoup-2.74.1/lib/girepository-1.0:/nix/store/g52b07pq7970x414y93r1x3kkrjhvqyb-evolution-data-server-3.42.3/lib/girepository-1.0:/nix/store/ivy5zrzrgs7khqjasmr963abj2zikhy6-gtk4-4.4.1/lib/girepository-1.0:/nix/store/5s37kdr0c27i59fyw29g3wch4jgyrfp3-gdm-41.0/lib/girepository-1.0:/nix/store/xpmvrqvyvxcr8nb36qs9qv79rmp1hb5l-geoclue-2.5.7/lib/girepository-1.0:/nix/store/51f8handwj0rxr5yxr24xnd322m3qh0j-gnome-bluetooth-3.34.5/lib/girepository-1.0:/nix/store/46cl35543lb70gfj9s6kfjiczwbcd0l1-at-spi2-core-2.42.0/lib/girepository-1.0:/nix/store/jbmzk88apy7spdn0gldq7bb7360c65dk-upower-0.99.13/lib/girepository-1.0:/nix/store/r0didybz3ma8jsv9h4f7prprv00xid9s-ibus-1.5.24/lib/girepository-1.0:/nix/store/q4jr8gbxd4156yxfqpkxapmw3qg86mr7-gnome-desktop-41.1/lib/girepository-1.0:/nix/store/w3273ya6xf3nqls2dbr5m6ck636hr1m9-gobject-introspection-1.70.0/lib/girepository-1.0:/nix/store/6hv1vafv0vkwmgwrg7jpfjv67b67yd7k-gstreamer-1.18.4/lib/girepository-1.0:/nix/store/5hrhffrdqywjdxbxx8q0j8vq12f9vwrh-gst-plugins-base-1.18.4/lib/girepository-1.0:/nix/store/gps9845as0z1hggs4165s8xvq9ch3jsf-libgweather-40.0/lib/girepository-1.0:/nix/store/ki5pcjfzm58a62ndclvsg74la65x4svw-libnma-1.8.32/lib/girepository-1.0:/nix/store/c9sibia5vr62j6w6s1f5h2gmqi2i863f-gnome-autoar-0.4.1/lib/girepository-1.0:/nix/store/b61xn0m9k3f1s51i2hhh48k3gb3z0a0f-telepathy-glib-0.24.2/lib/girepository-1.0:/nix/store/hrb6cjs0nk595pldgpsfypahvpap21dr-telepathy-logger-0.8.2/lib/girepository-1.0

Unfortunately, I am not actually sure what is causing this particular error – I was never able to reproduce it. I guess it may have to do with the typelibs being linked against different instance of one library.

Could you check which of the environment variables causes the trouble? If it is GI_TYPELIB_PATH, Could you run nix-store --query --requisites $(echo $GI_TYPELIB_PATH | sed 's/:/\n/g')?

For development, I would recommend clearing those environment variables. For running installed applications, we have an infrastructure that wraps programs in a way that puts package’s own dependencies to the beginning of the environment variable, so this type of conflict should not happen (though environment variables are evil and there is plethora of other ways it can bite you).

It was indeed GI_TYPELIB_PATH, here is the query output. It is on NixOS 21.11 stable with Gnome.

Thanks for the heads up!

It seems that I found the culprit here. I did a nixos-rebuild boot before running, so the system had older libs running compared to my app (which took the freshest from nixpkgs). When having the system fully upgraded and loaded into that configuration, the app runs normally. So nothing interesting here apparently.

Hmm, weird, I did not see multiple versions of any of the typelib path requisites. Maybe I should have specified to run it in the nix-shell.