簡體   English   中英

Python 3:使用 pip 安裝 gi 包

[英]Python 3: Installing gi package with pip

我正在嘗試使用 Python 3 運行Matplotlib 示例。要運行此示例,我需要先安裝gi (我正在使用pyenv ):

$ python --version
Python 3.6.1
$ pip --version
pip 9.0.1 from /home/hakon/.pyenv/versions/3.6.1/lib/python3.6/site-packages (python 3.6)
$ pip install gi
Collecting gi
  Downloading gi-1.2.tar.gz
Collecting requests (from gi)
  Downloading requests-2.16.0-py2.py3-none-any.whl (85kB)
    100% |████████████████████████████████| 92kB 959kB/s 
Collecting idna<2.6,>=2.5 (from requests->gi)
  Downloading idna-2.5-py2.py3-none-any.whl (55kB)
    100% |████████████████████████████████| 61kB 1.2MB/s 
Collecting chardet<3.1.0,>=3.0.2 (from requests->gi)
  Downloading chardet-3.0.3-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 1.8MB/s 
Collecting urllib3<1.22,>=1.21.1 (from requests->gi)
  Downloading urllib3-1.21.1-py2.py3-none-any.whl (131kB)
    100% |████████████████████████████████| 133kB 1.8MB/s 
Collecting certifi>=2017.4.17 (from requests->gi)
  Downloading certifi-2017.4.17-py2.py3-none-any.whl (375kB)
    100% |████████████████████████████████| 378kB 284kB/s 
Installing collected packages: idna, chardet, urllib3, certifi, requests, gi
  Running setup.py install for gi ... done
Successfully installed certifi-2017.4.17 chardet-3.0.3 gi-1.2 idna-2.5 requests-2.16.0 urllib3-1.21.1

現在,運行示例:

$ python toolmanager.py 
Traceback (most recent call last):
  File "./toolmanager.py", line 8, 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 10, in <module>
    import gi
  File "/home/hakon/.pyenv/versions/3.6.1/lib/python3.6/site-packages/gi/__init__.py", line 39
    print url
            ^
SyntaxError: Missing parentheses in call to 'print'

似乎pip以某種方式安裝了 Python 2 版本? 我該如何解決這個問題?

首先pip install gi會安裝另一個不相關的包,正確的名字是pgi 但是運行后:

$ 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.

似乎無法從 PyPI 安裝 Python 3 的pygobject 所以我嘗試從 Ubuntu 分發包python3-gi安裝所有東西:

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

這工作正常:)

要為標准 python 安裝, Håkon Hægland 答案是最佳選擇。

但是對於替代的 python 版本,可以使用pip<version> 請注意,必須使用備用 pip 來匹配備用 python。

文檔中給出了完整的解釋。

例如在 openSUSE(標准 python 版本 3.6,替代安裝 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
>>> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM