ValueError: Namespace Gtk is already loaded with version 3.0

I use ubuntu 22.04. When I try run supertool from terminal, I receive an error:

Traceback (most recent call last):
  File "/home/yurii/.local/share/gramps/gramps52/plugins/SuperTool/SuperTool.py", line 1183, in execute
    self.execute1(query)
  File "/home/yurii/.local/share/gramps/gramps52/plugins/SuperTool/SuperTool.py", line 1276, in execute1
    for values in gramps_engine.get_values(self.trans, result):
  File "/home/yurii/.local/share/gramps/gramps52/plugins/SuperTool/SuperTool.py", line 767, in get_values
    value, env = self.category.execute_func(
  File "/home/yurii/.local/share/gramps/gramps52/plugins/SuperTool/supertool_engine.py", line 746, in execute_person
    return execute(dbstate, obj, code, PersonProxy, envvars, exectype)
  File "/home/yurii/.local/share/gramps/gramps52/plugins/SuperTool/supertool_engine.py", line 731, in execute
    res = exec(code, env)
  File "initial_statements", line 2, in <module>
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 117, in require_version
    raise ValueError('Namespace %s is already loaded with version %s' %
ValueError: Namespace Gtk is already loaded with version 3.0
Traceback (most recent call last):
  File "/home/yurii/.local/share/gramps/gramps52/plugins/SuperTool/SuperTool.py", line 1183, in execute
    self.execute1(query)
  File "/home/yurii/.local/share/gramps/gramps52/plugins/SuperTool/SuperTool.py", line 1276, in execute1
    for values in gramps_engine.get_values(self.trans, result):
  File "/home/yurii/.local/share/gramps/gramps52/plugins/SuperTool/SuperTool.py", line 767, in get_values
    value, env = self.category.execute_func(
  File "/home/yurii/.local/share/gramps/gramps52/plugins/SuperTool/supertool_engine.py", line 746, in execute_person
    return execute(dbstate, obj, code, PersonProxy, envvars, exectype)
  File "/home/yurii/.local/share/gramps/gramps52/plugins/SuperTool/supertool_engine.py", line 731, in execute
    res = exec(code, env)
  File "initial_statements", line 2, in <module>
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 117, in require_version
    raise ValueError('Namespace %s is already loaded with version %s' %
ValueError: Namespace Gtk is already loaded with version 3.0
Looks like I have GTK versions conflict. I think Gramps uses another version then supertool. How can I fix this and dont break OS? Thanks
GRAMPS: 5.2.1 
Python: 3.10.12 
BSDDB: 6.2.9 (5, 3, 28) 
sqlite: 3.37.2 (2.6.0)
LANG: en_US.UTF-8
OS: Linux
Distribution: 6.8.0-40-generic

Please add how you are calling SuperTool from the terminal.

If that command line statement is executing a script, please include the script too.

And is Gramps running when the terminal call is made?

#!/bin/bash

# Get the current date and time
now=$(date +"%Y-%m-%d_%H-%M-%S")

echo "Run person names checker"
gramps -O "Main Tree" -a tool -p name=SuperTool,script=scripts/person_names_checker.script > "reports/person_names_checker_${now}.log" 2>&1

no, it is turned off.

I’ve simplified it, but still the same error. Looks like ubuntu upgrade has affect. I think a new one GTK version was installed on Ubuntu.

[Gramps SuperTool script file]
version=1

[title]
Surname Checker

[description]

[category]
People

[initial_statements]
print(f"-------------------Surnames Checker----------------------")

[statements]

[filter]

[expressions]

[scope]
all

[unwind_lists]
False

[commit_changes]
False

[summary_only]
True

Which version of SuperTool? (Displayed in lower right corner when SuperTool is open in Gramps.)

BTW, I am asking for these clarifications because @kku will probably need the information to suggest a solution. This problem is beyond my experience with SuperTool.

this is 1.4.1 version

Please update your 1.4.1 (6 May 2024) version of SuperTool addon and re-test. I am using the 1.4.9 version released 7 Aug 2024.

The update may not correct the issue. But we should be testing with the same version.

I’ve updated to 1.4.9. Still the same issue:

/usr/local/lib/python3.10/dist-packages/gramps/gui/plug/_guioptions.py:43: PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '4.0') before import to ensure that the right version gets loaded.
  from gi.repository import Gtk
ERROR: Failed reading plugin registration SuperTool.gpr.py
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/gramps/gen/plug/_pluginreg.py", line 1380, in scan_dir
    exec(
  File "SuperTool.gpr.py", line 26, in <module>
  File "/usr/local/lib/python3.10/dist-packages/gramps/gui/plug/__init__.py", line 31, in <module>
    from ._guioptions import make_gui_option, add_gui_options
  File "/usr/local/lib/python3.10/dist-packages/gramps/gui/plug/_guioptions.py", line 54, in <module>
    from .. import widgets
  File "/usr/local/lib/python3.10/dist-packages/gramps/gui/widgets/__init__.py", line 25, in <module>
    from .buttons import *
  File "/usr/local/lib/python3.10/dist-packages/gramps/gui/widgets/buttons.py", line 50, in <module>
    class IconButton(Gtk.Button):
  File "/usr/local/lib/python3.10/dist-packages/gramps/gui/widgets/buttons.py", line 51, in IconButton
    def __init__(self, func, handle, icon="gtk-edit", size=Gtk.IconSize.MENU):
AttributeError: type object 'IconSize' has no attribute 'MENU'

2024-08-29 10:43:47.549: WARNING: _manager.py: line 329: Plugin error (from 'degreesofseparation'): Namespace Gtk is already loaded with version 4.0
2024-08-29 10:43:47.550: ERROR: grampsapp.py: line 188: Unhandled exception
Traceback (most recent call last):
  File "/usr/local/bin/gramps", line 3, in <module>
    app.main()
  File "/usr/local/lib/python3.10/dist-packages/gramps/grampsapp.py", line 686, in main
    errors = run()
  File "/usr/local/lib/python3.10/dist-packages/gramps/grampsapp.py", line 678, in run
    startcli(error, argpars)
  File "/usr/local/lib/python3.10/dist-packages/gramps/cli/grampscli.py", line 371, in startcli
    climanager.do_reg_plugins(dbstate, uistate=None)
  File "/usr/local/lib/python3.10/dist-packages/gramps/cli/grampscli.py", line 334, in do_reg_plugins
    self._pmgr.reg_plugins(USER_PLUGINS, dbstate, uistate, load_on_reg=True)
  File "/usr/local/lib/python3.10/dist-packages/gramps/gen/plug/_manager.py", line 230, in reg_plugins
    r_class = getattr(mod, plugin.ruleclass)
AttributeError: 'NoneType' object has no attribute 'DegreesOfSeparation'

I have investigated this for a while but haven’t been able to reproduce the problem. Have you found a solution?

Does Gramps work normally in GUI mode? Do other tools work from the command line?

I don’t use the latest Ubuntu, nor Mint 22 for that matter, but it looks like we need to make sure that all imports require Gtk 3, and not 4.

I have no clue abou that attribute error, other than that the code may need to check for None in more places.

Unfortunately not yet.

Yes, the same scripts work nice in GUI mode

The commands like this one work good:
gramps 'Romanenko Tree'

I can give more info. My system has 3 Gtk versions:

dpkg -l | grep libgtk

And looks like my Gramps app uses Gtk 3.

lsof -p 100846 | grep Gtk

here 100846 - is Gramps PID.

I receive this one:

gramps  100846 yurii  mem       REG              252,0   678348 29238900 /usr/lib/x86_64-linux-gnu/girepository-1.0/Gtk-3.0.typelib

My guess is that this is because ‘gi.require_version(“3.0”)’ is not called when Gramps is run in the command line mode. There is such a call in gramps/gui/grampsgui.py but not in gramps/cli/grampscli.py.

Maybe you can try the following. The file /usr/bin/gramps contains

#!/usr/bin/python3 -O
import gramps.grampsapp as app
app.main()

Edit that file (or make a copy) so that it will contain

#!/usr/bin/python3 -O
import gi
gi.require_version("Gtk", "3.0")
import gramps.grampsapp as app
app.main()

This should force Gramps to use Gtk3 instead of Gtk4. This still does not explain why I could not reproduce…

But exception tells about Gtk 4. Looks like Supertool (via terminal) tries import Gtk 4 (the last), but Gramps already uses Gtk 3.

Chat GPT proposes require Gtk like this:

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

But I am not sure this is good solution. Can Supertool check which Gtk version is alreaty imported by Gramps?

As far as I understand Gramps uses and requires Gtk 3.0. It does not work with any other version.

Supertool itself does not specify any version. It relies on Gramps choosing the correct version (3.0).

It seems that in Ubuntu 22.04 the default Gtk version is 4.0. Therefore Gramps needs to specify the correct version (3.0). But it looks like it doesn’t (always?) do that if run in the command line mode.

got it

got it

Looks like you are very close to the true.

So, do you think the solution could be ?

I will try it tomorrow. Thank you a lot!

I tried it now. The terminal supertool works good now. The issue is solved. But what I think, If Gramps has strict dependency of GTK version, maybe it should be added for all Gramps users in the future versions?

gi.require_version("Gtk", "3.0")

and later

gi.require_version("Gtk", "4.0")

1 Like

I guess this is something that the Gramps core developers can decide.

1 Like

@Nick-Hall could you make a review of current problem pls? Looks like we have a solution, but it makes some core changes.