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/", 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/", 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/", line 2500, in <module>
  File "/nix/store/mar5qp8knkprymbany0h1ildlz5lf7i3-python3-3.9.6-env/lib/python3.9/site-packages/matplotlib/", 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/", line 278, in backend_mod
  File "/nix/store/19j43hyh37jqnn9l7rg6shk5barbn209-python3-3.9.6/lib/python3.9/importlib/", 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/", 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/", line 28, in <module>
    raise ImportError from e

This is my current python config in environment.systemPackages:

      my-python-packages = python-packages: with python-packages; [
        # Scientific libraries
        # Gtk hell
      python-with-my-packages = python3.withPackages my-python-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.


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; [
  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

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