Python matplotlib Gtk backend not working

Hello community,

I am trying to set up my Python scientific environment in NixOS 21.11, and I have stumbled against some issues with the matplotlib library. More specifically, I get the following error when trying to use the Gtk backend:

$ python -c 'import matplotlib.pyplot as plt; plt.plot()'
Traceback (most recent call last):
  File "/nix/store/mar5qp8knkprymbany0h1ildlz5lf7i3-python3-3.9.6-env/lib/python3.9/site-packages/matplotlib/backends/backend_gtk3.py", line 24, in <module>
    gi.require_version("Gtk", "3.0")
  File "/nix/store/mar5qp8knkprymbany0h1ildlz5lf7i3-python3-3.9.6-env/lib/python3.9/site-packages/gi/__init__.py", line 126, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gtk not available

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/nix/store/mar5qp8knkprymbany0h1ildlz5lf7i3-python3-3.9.6-env/lib/python3.9/site-packages/matplotlib/pyplot.py", line 2500, in <module>
    switch_backend(rcParams["backend"])
  File "/nix/store/mar5qp8knkprymbany0h1ildlz5lf7i3-python3-3.9.6-env/lib/python3.9/site-packages/matplotlib/pyplot.py", line 277, in switch_backend
    class backend_mod(matplotlib.backend_bases._Backend):
  File "/nix/store/mar5qp8knkprymbany0h1ildlz5lf7i3-python3-3.9.6-env/lib/python3.9/site-packages/matplotlib/pyplot.py", line 278, in backend_mod
    locals().update(vars(importlib.import_module(backend_name)))
  File "/nix/store/19j43hyh37jqnn9l7rg6shk5barbn209-python3-3.9.6/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/nix/store/mar5qp8knkprymbany0h1ildlz5lf7i3-python3-3.9.6-env/lib/python3.9/site-packages/matplotlib/backends/backend_gtk3agg.py", line 8, in <module>
    from . import backend_agg, backend_gtk3
  File "/nix/store/mar5qp8knkprymbany0h1ildlz5lf7i3-python3-3.9.6-env/lib/python3.9/site-packages/matplotlib/backends/backend_gtk3.py", line 28, in <module>
    raise ImportError from e
ImportError

This is my current python config in environment.systemPackages:

    (let
      my-python-packages = python-packages: with python-packages; [
        # Scientific libraries
        pandas
        numpy
        scipy
        matplotlib
        jupyter
        ipython
        # Gtk hell
        pygobject3
        pycairo
      ];
      python-with-my-packages = python3.withPackages my-python-packages;
    in
      python-with-my-packages)

I have also in installed the packages gtk3, cairo, and gobject-introspection, but I still get the same error. Any hint on how to solve this issue? Thanks in advance.

Hi!

I tried around for half an hour now, and give up. I thought, hey this is an easy one, but nope. To be honest, I think this is bugged, but let’s wait for other opinions. What I got:

  packageOverrides = self: super: {
    matplotlib = super.matplotlib.override { enableGtk3 = true; };
  };
  myPython = pkgs.python3.override { inherit packageOverrides; };
  myPythonPackages = ppkgs: with ppkgs; [
    ipython
    matplotlib
  ];
  myPythonEnvironment = myPython.withPackages myPythonPackages;

This works somewhat. It definitely sets the flag, and rebuilds a lot of things when adding other Python packages (sucks). However, I get the same error as you do. So it seems that there is a deeper problem.

For reference, the Python code to get the error message:

import matplotlib
import matplotlib.pyplot as plt
matplotlib.use("GTK3Cairo")

EDIT: Maybe it is worth trying a system-wide overlay. I didn’t use a system-wide overlay here.

Anybody? Should we move this to the Git repo issue list?

AFAIK, there’s nothing against it in this case. Just go ahead and file an issue.

The current maintainers of python3Packages.matplotlib are lovek323 and veprbl. There will be a section inside the issue template for you to ping them.

This is a well known issue, you need GI_TYPELIB_PATH set. We have an infrastructure for that but it relies on wrappers so it can only be done on a leaf package that is a program.

If you search, you should see several issues and discourse threads talking about this (e.g python{2,3}.pkgs.gobject3 with GTK3 - not found? · Issue #39637 · NixOS/nixpkgs · GitHub).