Python 3: Installing gi package with pip

前端 未结 2 544
名媛妹妹
名媛妹妹 2020-12-16 19:41

I am trying to run this Matplotlib example using Python 3. To run this I needed to install gi first (I am using pyenv):

$ python --         


        
相关标签:
2条回答
  • 2020-12-16 20:19

    To install for the standard python, Håkon Hægland answer is the best choice.

    But for an alternate python version, one can use pip<version>. Beware that the alternate pip has to be used to match the alternate python.

    The full explanations are given in the documentation.

    For instance on openSUSE (standard python version 3.6, alternate installed 3.8):

    > sudo zypper install cairo-devel pkg-config python3-devel gcc gobject-introspection-devel
    > pip3.8 install --user pycairo
    > pip3.8 install --user PyGObject
    > python3.8
    Python 3.8.1 (default, Feb  1 2020, 14:50:41) 
    [GCC 7.5.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import gi
    >>> 
    
    0 讨论(0)
  • 2020-12-16 20:31

    First, pip install gi will install another unrelated package, the correct name is pgi. But after running:

    $ pip uninstall gi
    $ pip install pgi
    $ python toolmanager.py
    [...]
    Traceback (most recent call last):
      File "toolmanager.py", line 14, in <module>
        import matplotlib.pyplot as plt
      File "/home/hakon/.pyenv/versions/3.6.1/lib/python3.6/site-packages/matplotlib/pyplot.py", line 115, in <module>
        _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
      File "/home/hakon/.pyenv/versions/3.6.1/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
        globals(),locals(),[backend_name],0)
      File "/home/hakon/.pyenv/versions/3.6.1/lib/python3.6/site-packages/matplotlib/backends/backend_gtk3cairo.py", line 6, in <module>
        from . import backend_gtk3
      File "/home/hakon/.pyenv/versions/3.6.1/lib/python3.6/site-packages/matplotlib/backends/backend_gtk3.py", line 12, in <module>
        raise ImportError("Gtk3 backend requires pygobject to be installed.")
    ImportError: Gtk3 backend requires pygobject to be installed.
    

    It seems that pygobject for Python 3 cannot be installed from PyPI. So I tried to install everything from the Ubuntu distribution package python3-gi instead:

    $ sudo apt-get install python3-gi
    $ pyenv local system
    $ python3 --version
    Python 3.5.3
    $ python3 toolmanager.py
    

    and this works fine :)

    0 讨论(0)
提交回复
热议问题