如何使用pip安装cairocffi?

16

如何通过pip安装cairocffi?

cairocffi是基于CFFI的可替代Pycairo的库,具体请访问https://github.com/SimonSapin/cairocffi

我正尝试在Ubuntu 14.04上安装它:

alvas@ubi:~$ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.2 LTS"
NAME="Ubuntu"
VERSION="14.04.2 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.2 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

我尝试使用标准的pip命令进行安装,但出现了这个错误:

$ sudo pip install cairocffi
The directory '/home/alvas/.cache/pip/log' or its parent directory is not owned by the current user and the debug log has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/alvas/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/alvas/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting cairocffi
  Downloading cairocffi-0.6.tar.gz (75kB)
    100% |████████████████████████████████| 77kB 34kB/s 
Collecting cffi>=0.6 (from cairocffi)
  Downloading cffi-0.9.2.tar.gz (209kB)
    100% |████████████████████████████████| 212kB 97kB/s 
Requirement already satisfied (use --upgrade to upgrade): pycparser in /usr/local/lib/python3.4/dist-packages (from cffi>=0.6->cairocffi)
Installing collected packages: cffi, cairocffi
  Running setup.py install for cffi
    Complete output from command /usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-d3kjzf__/cffi/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-ll323a3c-record/install-record.txt --single-version-externally-managed --compile:
    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
    No package 'libffi' found
    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
    No package 'libffi' found
    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
    No package 'libffi' found
    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
    No package 'libffi' found
    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
    No package 'libffi' found
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.4
    creating build/lib.linux-x86_64-3.4/cffi
    copying cffi/commontypes.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/lock.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/api.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/verifier.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/__init__.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/cparser.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/backend_ctypes.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/vengine_gen.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/gc_weakref.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/ffiplatform.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/model.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/vengine_cpy.py -> build/lib.linux-x86_64-3.4/cffi
    running build_ext
    building '_cffi_backend' extension
    creating build/temp.linux-x86_64-3.4
    creating build/temp.linux-x86_64-3.4/c
    x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -DUSE__THREAD -I/usr/include/ffi -I/usr/include/libffi -I/usr/include/python3.4m -c c/_cffi_backend.c -o build/temp.linux-x86_64-3.4/c/_cffi_backend.o
    c/_cffi_backend.c:13:17: fatal error: ffi.h: No such file or directory
     #include <ffi.h>
                     ^
    compilation terminated.
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

    ----------------------------------------
    Command "/usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-d3kjzf__/cffi/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-ll323a3c-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-d3kjzf__/cffi

我已经手动检查了权限,发现没有写入权限。为什么会这样?而且为什么sudo命令无法覆盖权限?

$ ls -la .cache/pip/log/
total 60
drwxrwxr-x 2 alvas alvas  4096 Feb  3 10:51 .
drwx------ 4 alvas alvas  4096 Apr 12 23:16 ..
-rw-rw-r-- 1 alvas alvas 49961 Apr 12 23:18 debug.log

当我尝试使用sudo -H pip install cairoffi时,我得到了以下结果:

sudo -H pip install cairocffi
Collecting cairocffi
  Using cached cairocffi-0.6.tar.gz
Collecting cffi>=0.6 (from cairocffi)
  Downloading cffi-0.9.2.tar.gz (209kB)
    100% |████████████████████████████████| 212kB 29kB/s 
Requirement already satisfied (use --upgrade to upgrade): pycparser in /usr/local/lib/python3.4/dist-packages (from cffi>=0.6->cairocffi)
Installing collected packages: cffi, cairocffi
  Running setup.py install for cffi
    Complete output from command /usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-2sv6pbsp/cffi/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-xk4kkjrj-record/install-record.txt --single-version-externally-managed --compile:
    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
    No package 'libffi' found
    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
    No package 'libffi' found
    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
    No package 'libffi' found
    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
    No package 'libffi' found
    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
    No package 'libffi' found
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.4
    creating build/lib.linux-x86_64-3.4/cffi
    copying cffi/commontypes.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/lock.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/api.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/verifier.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/__init__.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/cparser.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/backend_ctypes.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/vengine_gen.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/gc_weakref.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/ffiplatform.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/model.py -> build/lib.linux-x86_64-3.4/cffi
    copying cffi/vengine_cpy.py -> build/lib.linux-x86_64-3.4/cffi
    running build_ext
    building '_cffi_backend' extension
    creating build/temp.linux-x86_64-3.4
    creating build/temp.linux-x86_64-3.4/c
    x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -DUSE__THREAD -I/usr/include/ffi -I/usr/include/libffi -I/usr/include/python3.4m -c c/_cffi_backend.c -o build/temp.linux-x86_64-3.4/c/_cffi_backend.o
    c/_cffi_backend.c:13:17: fatal error: ffi.h: No such file or directory
     #include <ffi.h>
                     ^
    compilation terminated.
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

    ----------------------------------------
    Command "/usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-2sv6pbsp/cffi/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-xk4kkjrj-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-2sv6pbsp/cffi

正如 @MattDMo 建议的那样,我尝试过 apt-get install libffi 但它仍然没有成功:

alvas@ubi:~$ sudo apt-get install libffi libffi-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package libffi

但是包管理器上没有任何 libffi ,所以我尝试了 libffi-dev

alvas@ubi:~$ sudo apt-get install libffi-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  libffi-dev
0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 99.8 kB of archives.
After this operation, 323 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libffi-dev amd64 3.1~rc1+r3.0.13-12 [99.8 kB]
Fetched 99.8 kB in 1s (76.3 kB/s)     
Selecting previously unselected package libffi-dev:amd64.
(Reading database ... 492855 files and directories currently installed.)
Preparing to unpack .../libffi-dev_3.1~rc1+r3.0.13-12_amd64.deb ...
Unpacking libffi-dev:amd64 (3.1~rc1+r3.0.13-12) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Processing triggers for doc-base (0.10.5) ...
Processing 1 added doc-base file...
Processing triggers for install-info (5.2.0.dfsg.1-2) ...
Setting up libffi-dev:amd64 (3.1~rc1+r3.0.13-12) ...

它成功安装了libffi-dev,但是cairoffi仍然无法安装:

alvas@ubi:~$ sudo -H pip install cairoffi
Collecting cairoffi
  Could not find a version that satisfies the requirement cairoffi (from versions: )
  No matching distribution found for cairoffi
alvas@ubi:~$ sudo -H pip3 install cairoffi
Collecting cairoffi
  Could not find a version that satisfies the requirement cairoffi (from versions: )
  No matching distribution found for cairoffi

就像我说的那样,您需要使用Linux发行版的软件包管理器(yumapt-get等)来安装系统软件包libffilibffi-dev。如果我的表述不够清晰,我很抱歉。 - MattDMo
另外,下次如果您对答案有疑问或问题,请在答案下方发表评论,而不仅仅是编辑您的问题,即使只是指向编辑。我之所以看到这个是因为我无意中把标签页留开了。发表评论会向作者和任何您可能提及的人发送通知。 - MattDMo
3个回答

16

错误信息中已经明确写着:

No package 'libffi' found

pip安装之前,您需要通过系统包管理器(yum, apt-get, 或其他)安装libffilibffi-dev。它们的名称可能会因平台而异。


@alvas,你使用的是哪个发行版和版本? - MattDMo
7
在Ubuntu上,这些包是libffi6libffi-dev。运行sudo apt-get install libffi6 libffi-dev应该可以正常工作... - MattDMo
它说已经安装了,但cairoffi仍然不满足。 - alvas
1
@alvas,两个都已经安装了吗?您可能需要重新启动终端会话(可能有效),注销并重新登录(稍微有更大的机会有效),或者重启(几乎肯定应该有效)。在此之前,您可以尝试运行sudo ldconfig,这可能会奏效。还要确保您已经使用apt-get安装了python3-dev。如果您已经成功编译了其他本地Python扩展程序,则可能已经安装了它,但最好检查一下。除此之外,尝试阅读这篇文章,看看是否能找到任何有用的信息。 - MattDMo
1
@alvas 哦,我刚发现了一些东西 - python3-cffi!使用 apt-get 安装它,你就可以开始了! - MattDMo
1
@alvas和我刚刚看了你的最新更新 - 你试图使用pip安装cairoffi而不是cairocffi。尝试使用后者... - MattDMo

15

9

对于我来说,在使用python 3.5.3rc1的Windows 10上,pip install cairocffi 失败了。而执行 pip install cffi 就足以让它成功。


对我来说,它会给出 AssertionError: 未找到 cairocffi .dist-info 目录。 - Mojimi
这对我有用 @Mojimi https://github.com/Kozea/cairocffi/issues/125#issuecomment-476295293 - A T

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接