【问题标题】:django OSError: no library called "cairo" was found on windowsdjango OSError:在 Windows 上找不到名为“cairo”的库
【发布时间】:2020-04-16 06:45:17
【问题描述】:

当我运行 Django 服务器时,我看到了这个问题!!

OSError: no library called "cairo" was found

no library called "libcairo-2" was found

cannot load library 'libcairo.so': error 0x7e

cannot load library 'libcairo.2.dylib': error 0x

cannot load library 'libcairo-2.dll': error 0x7e

【问题讨论】:

  • 请您提供更多详细信息,例如 Djano 版本、Windows 版本。
  • pip freeze 的输出在这里很有用。你是如何运行你的 Django 项目的?使用 manage.py?
  • 能否获取操作系统及其版本?
  • 您在项目中使用 weasyprint 吗?
  • 我面临同样的问题,我正在使用 django-weasyprint。

标签: python windows pip cairo pycairo


【解决方案1】:

安装 GTK+ 对我不起作用。 我使用UniConverter2.0 解决了这个问题。 我的环境是

  • Python 3.7
  • Windows 10 x64
  1. 安装uniconvertor-2.0rc4-win64_headless.msi,
  2. 找到UniConverter安装路径下的“dll”子目录。(在我的例子中,C:\Program Files\UniConvertor-2.0rc4\dlls
  3. 将此“dll”路径添加到系统路径。
  4. 关闭 VSCode 并重新打开项目。
  5. 尝试再次运行服务器。 尽情享受吧!

【讨论】:

    【解决方案2】:

    WeasyPrint 需要 Pango、cairo 和 GDK-PixBuf 库。它们是 GTK+(以前称为 GIMP Toolkit)的一部分,必须单独安装。

    安装GTK+ libraries后,做:

    python -m weasyprint http://weasyprint.org weasyprint.pdf
    

    【讨论】:

    • 安装 GTK 后我仍然收到此错误
    • 不要忘记将安装的 GTK+ lib 添加到您的系统路径,并重新启动您的 IDE 以使更改生效。
    • @Mathador 同意。
    • @Mathador 我如何将它们添加到路径中?
    • 按照此处的说明进行操作:“gisaxs.com/index.php/View.gtk_installation”并将“C:\GTK\bin”替换为您在驱动器上安装 GTK 包的实际路径。
    【解决方案3】:

    从 Python 3.8 开始,需要单独添加 dll。 添加了 GTK+、MSYS2、Visual Studio C 编译器和单转换器。但是,似乎没有任何效果。 最后,把调用add_dll_directory的脚本放好后就可以运行了。

    import os
    
    def set_dll_search_path():
       # Python 3.8 no longer searches for DLLs in PATH, so we have to add
       # everything in PATH manually. Note that unlike PATH add_dll_directory
       # has no defined order, so if there are two cairo DLLs in PATH we
       # might get a random one.
       if os.name != "nt" or not hasattr(os, "add_dll_directory"):
           return
       for p in os.environ.get("PATH", "").split(os.pathsep):
           try:
               os.add_dll_directory(p)
           except OSError:
               pass
    
    
    set_dll_search_path()
    

    来源:PyCairo Windows Python3.8 Import Issue

    【讨论】:

    • 天哪,这是为我解决的问题。谢谢!
    【解决方案4】:

    在此处查看解决方案:

    https://www.programmersought.com/article/47674569357/

    如果安装后没有添加,则需要添加路径:

    C:\Program Files\GTK3-Runtime Win64\bin

    我已经用这种方法解决了很多次了。

    【讨论】:

      【解决方案5】:

      好的,我想通了。你可以有一个不起作用的 64 位版本的 python。我发现可以工作并且可能会改变的是从 python 的网站安装 64 位版本的 python,而不是从 Microsoft 商店!

      1. 删除您已安装的任何 python 版本
      2. 下载https://www.python.org/ftp/python/3.8.10/python-3.8.10-amd64.exe
      3. 安装
      4. 下载安装https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer/releases
        1. 确切的链接是https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer/releases/download/2021-04-29/gtk3-runtime-3.24.29-2021-04-29-ts-win64.exe
        2. 注意链接来自https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#windows
      5. 在您的环境路径中添加 bin 文件夹(抱歉,这是别人的机器,我使用的是 Ubuntu)。
      6. 完成 weasyprint 的第一步,您应该一切顺利!

      【讨论】:

      • 是的,成功了!似乎是从 Microsoft Store 安装的 Python 的问题。我使用了 Python 3.10.1,它对我有用。也许只是不要直接链接到特定的 Python 安装程序,只需像您为 GTK 安装程序所做的那样指向官方网站。另请注意,这种安装 Python 的方式不提供 python3 命令行别名。 Just use python instead.
      【解决方案6】:

      可能有点晚了,但我刚刚遇到了同样的问题,并且: 从这里开始:https://weasyprint.readthedocs.io/en/stable/install.html#windows。在哪里可以找到包含所需 DLL 的 GTK 包的链接。

      就我而言,我有一个 64 位 Python,所以我使用:“Download and run the latest gtk3-runtime-x.x.x-x-x-x-ts-win64.exe

      我没有更改安装目录

      安装完成后。我将路径添加到我的变量路径。

      我重新启动了终端,确保可以使用:WHERE libcairo-2.dll 找到 DLL。这返回了C:\Program Files\GTK3-Runtime Win64\bin\libcairo-2.dll

      然后我运行python -m weasyprint http://weasyprint.org weasyprint.pdf 并收到了一些警告,但它们只是警告:)

      【讨论】:

        【解决方案7】:

        我也遇到了同样的错误

        我已经按照gtk install in windows安装了gtk

        没有任何效果

        之后:

        python -m pip install pycairo

        帮我解决了问题

        【讨论】:

          【解决方案8】:

          如果您使用的是轻量级 Linux Docker Image,它可能不包含上面所说的 GTK,那么您可以通过添加到您的 Dockerfile 中来包含它

          RUN apt-get update -y
          RUN apt-get install python3-cffi python3-brotli libpango-1.0-0 libharfbuzz0b libpangoft2-1.0-0 libgtk-3-dev gcc -y
          

          weasyprint 推荐安装所有这些软件包,而 GCC 有 GTK。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-02-16
            • 2020-03-15
            • 2016-11-28
            • 2019-05-12
            • 1970-01-01
            • 2018-08-14
            • 2019-11-20
            • 1970-01-01
            相关资源
            最近更新 更多