File: imports.rst

package info (click to toggle)
pygobject 3.55.3-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,000 kB
  • sloc: ansic: 39,431; python: 26,883; sh: 114; makefile: 81; xml: 35; cpp: 1
file content (61 lines) | stat: -rw-r--r-- 1,683 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Imports
=======

The toplevel ``gi`` package allows you to import the different libraries
namespaces and ensure specific versions of them.

The next code line will import the ``GTK`` and ``GLib`` libraries from the
``gi.repository`` module. ``gi.repository`` holds the libraries bindings.

.. code:: python

    from gi.repository import Gtk, GLib


If you want to ensure a specific version of a library you can use :func:`gi.require_version`.

.. code:: python

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


Currently, when importing ``Gdk`` or ``Gtk``, their init functions
(``Gdk.init_check()`` and ``Gtk.init_check()`` respectively) will be
automatically called for backwards-compatibility reasons.
This prevents, among other things, to use ``Gtk.disable_setlocale()``, as
it shall be called before ``Gtk`` initialization.

This behavior may be dropped in the future, but in the meanwhile you can
use :func:`gi.disable_legacy_autoinit` before the import to skip the
auto-init.

.. code:: python

    import gi
    gi.disable_legacy_autoinit()
    from gi.repository import Gtk


To avoid `PEP8/E402 <https://www.flake8rules.com/rules/E402.html>`_ you can
use a try block.

.. code:: python

    import sys

    import gi
    try:
        gi.require_version('Gtk', '4.0')
        gi.require_version('Adw', '1')
        from gi.repository import Adw, Gtk
    except ImportError or ValueError as exc:
        print('Error: Dependencies not met.', exc)
        sys.exit(1)


.. seealso::
    For more detailed information of the methods provided by the ``gi`` module
    checkout :ref:`guide-api`.