无法通过pip3安装pycairo

时间:2019-05-03 16:02:57

标签: python-3.x pkg-config pycairo

我一直试图在MacO上使用pip3安装pycairo,但是每当尝试这样做时,就会出现此错误:

pip3 install pycairo
Collecting pycairo
  Using cached https://files.pythonhosted.org/packages/48/20/5e83af98eb897935bf7dc39455e892ba866feebb9b7c3b392982866f9958/pycairo-1.18.1.tar.gz
Installing collected packages: pycairo
  Running setup.py install for pycairo ... error
    ERROR: Complete output from command /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 -u -c 'import setuptools, tokenize;__file__='"'"'/private/var/folders/0w/2v91jtp54jl7b4cdqrhk_96h0000gn/T/pip-install-uiqmlfkz/pycairo/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/0w/2v91jtp54jl7b4cdqrhk_96h0000gn/T/pip-record-egwlnpkb/install-record.txt --single-version-externally-managed --compile:
    ERROR: running install
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.9-x86_64-3.7
    creating build/lib.macosx-10.9-x86_64-3.7/cairo
    copying cairo/__init__.py -> build/lib.macosx-10.9-x86_64-3.7/cairo
    copying cairo/__init__.pyi -> build/lib.macosx-10.9-x86_64-3.7/cairo
    copying cairo/py.typed -> build/lib.macosx-10.9-x86_64-3.7/cairo
    running build_ext
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    Package 'libffi', required by 'gobject-2.0', not found
    Command '['pkg-config', '--print-errors', '--exists', 'cairo >= 1.13.1']' returned non-zero exit status 1.
    ----------------------------------------
ERROR: Command "/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 -u -c 'import setuptools, tokenize;__file__='"'"'/private/var/folders/0w/2v91jtp54jl7b4cdqrhk_96h0000gn/T/pip-install-uiqmlfkz/pycairo/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/0w/2v91jtp54jl7b4cdqrhk_96h0000gn/T/pip-record-egwlnpkb/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/0w/2v91jtp54jl7b4cdqrhk_96h0000gn/T/pip-install-uiqmlfkz/pycairo/

我已经使用brew安装了cairo和pkg-config。

更新,我确实使用以下命令设置了PKG_CONFIG_PATH的路径

export PKG_CONFIG_PATH=/usr/local/Cellar/libffi/3.2.1/lib/pkgconfig/

然后使用pip3安装pycairo即可完成工作。唯一的问题是,即使现在安装了python3也找不到pycairo。

python3
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pycairo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pycairo'

我在brew上安装了以下软件

brew list
aom         jpeg            p11-kit
atk         lame            pango
cairo           leptonica       pcre
ffmpeg          libass          pixman
flac            libbluray       pkg-config
fontconfig      libevent        python
freetype        libffi          readline
frei0r          libogg          rtmpdump
fribidi         libpng          rubberband
gdbm            libsamplerate       sdl2
gdk-pixbuf      libsndfile      snappy
gettext         libsoxr         speex
ghostscript     libtasn1        sqlite
giflib          libtiff         tesseract
glew            libunistring        theora
glib            libvorbis       unbound
gmp         libvpx          webp
gnutls          little-cms2     x264
graphite2       nettle          x265
gtk+            opencore-amr        xvid
harfbuzz        openjpeg        xz
hicolor-icon-theme  openssl
icu4c           opus

这是我的pip3列表

astroid           2.2.5   
cairocffi         1.0.2   
cffi              1.12.3  
isort             4.3.16  
lazy-object-proxy 1.3.1   
mccabe            0.6.1   
opencv-python     3.4.2.17
pip               19.1    
pycairo           1.18.1  #it seems like it's installed but it doesn't recognize it whenever I try to import it
pycparser         2.19    
pylint            2.3.1   
setuptools        40.8.0  
typed-ast         1.3.1   
wrapt             1.11.1  

3 个答案:

答案 0 :(得分:1)

有同样的问题。

解决方案:

保留通过brew安装的python3和libffi。仅在安装cairo之前执行此操作:

export LDFLAGS="-L/usr/local/opt/libffi/lib"
export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig"

现在继续运行pip:

pip3 install --user pycairo

一切都应在此之后进行。

在macOS 10.14.6上测试,通过brew安装的python 3.7.4,通过brew安装的libffi 3.2.1。

答案 1 :(得分:0)

pip3输出中,您可以在那里找到答案:

  

Package 'libffi', required by 'gobject-2.0', not found

在macOS上,brew install libffi应该这样做。

(当然,将任何东西安装到系统Python中,而不是安装在virtualenv内,通常也是个坏主意。)

答案 2 :(得分:0)

由于任何我无法理解的原因,卸载python3的brew版本并手动安装可以解决它。无论如何,在VSC中使用cairo时,它告诉我cairo模块内不存在任何成员,但它仍然能够正常工作。