[Python] Issues with converthing a python dictionary to a GLib Variant

Discussão em 'Python' iniciado por Stack, Julho 22, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use the NetworkManager DBus method AddAndActivateConnection with the pydbus library to connect to a Wi-Fi network. But it's giving me a lot of trouble in generating the proper GLib Variant based on the format specifier: a{sa{sv}}

    from pydbus import SystemBus

    bus = SystemBus()
    nm = bus.get("org.freedesktop.NetworkManager")

    # Find Wi-Fi device
    wifi_path = None
    for dev_path in nm.GetDevices():
    dev = bus.get("org.freedesktop.NetworkManager", dev_path)
    if dev.DeviceType == 2:
    wifi_path = dev_path
    break

    if not wifi_path:
    raise RuntimeError("No Wi-Fi device found.")

    ssid = "MyWiFi"
    password = "mypassword123"

    settings = {
    'connection': {
    'id': ssid,
    'type': '802-11-wireless',
    },
    '802-11-wireless': {
    'ssid': ssid,
    'mode': 'infrastructure',
    },
    '802-11-wireless-security': {
    'key-mgmt': 'wpa-psk',
    'psk': password,
    },
    'ipv4': {
    'method': 'auto',
    },
    'ipv6': {
    'method': 'ignore',
    }
    }

    nm.AddAndActivateConnection(settings, wifi_path, "/")


    This results in this error in GLib:

    Traceback (most recent call last):
    File "/root/connect3.py", line 43, in <module>
    nm.AddAndActivateConnection(settings, wifi_path, "/")
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/lib/python3/dist-packages/pydbus/proxy_method.py", line 74, in __call__
    self._iface_name, self.__name__, GLib.Variant(self._sinargs, args), GLib.VariantType.new(self._soutargs),
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
    File "/usr/lib/python3/dist-packages/gi/overrides/GLib.py", line 189, in __new__
    v = creator._create(format_string, value)
    File "/usr/lib/python3/dist-packages/gi/overrides/GLib.py", line 150, in _create
    builder.add_value(self._create(dup, i))
    ~~~~~~~~~~~~^^^^^^^^
    File "/usr/lib/python3/dist-packages/gi/overrides/GLib.py", line 145, in _create
    builder.add_value(self._create(element_type, i))
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
    File "/usr/lib/python3/dist-packages/gi/overrides/GLib.py", line 150, in _create
    builder.add_value(self._create(dup, i))
    ~~~~~~~~~~~~^^^^^^^^
    File "/usr/lib/python3/dist-packages/gi/overrides/GLib.py", line 145, in _create
    builder.add_value(self._create(element_type, i))
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
    File "/usr/lib/python3/dist-packages/gi/overrides/GLib.py", line 150, in _create
    builder.add_value(self._create(dup, i))
    ~~~~~~~~~~~~^^^^^^^^
    File "/usr/lib/python3/dist-packages/gi/overrides/GLib.py", line 118, in _create
    return self._LEAF_CONSTRUCTORS[format](value)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
    TypeError: argument value: Expected GLib.Variant, but got str


    I've tried using GLib.Variant() to pre package the settings dictionary but I just cannot find a working combination. I've tried debugging it with pdb/pudb but I can't quite grasp what is happing in the library.

    I'm just lost at this point, I've been trying everything I can think of but just can't get it to work.

    Can someone explain what I am doing wrong?

    System info: arch: aarch64 OS: Debian 13 (trixie) Python version: 3.13.5

    Continue reading...

Compartilhe esta Página