首页 > 解决方案 > Why I get an ImportError while importing cairo if the installation seems to be correct?

问题描述

The issue is quite simple. I've installed Python and Cairo with Homebrew since I use a M1 MacBook. When trying to import the package, I get this error:

>>> import cairo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/cairo/__init__.py", line 1, in <module>
    from ._cairo import *  # noqa: F401,F403
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/cairo/_cairo.cpython-39-darwin.so, 2): Symbol not found: _cairo_append_path
  Referenced from: /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/cairo/_cairo.cpython-39-darwin.so
  Expected in: flat namespace
 in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/cairo/_cairo.cpython-39-darwin.so

Thank you in advance.

标签: pythoncairopycairo

解决方案


Looking at this issue over on their GitHub, and in particular this comment, you could try a couple things:

  • Use Python 3.9 if you're not already (and use python3 in the terminal)
  • Uninstall pycairo as you have it now and install py3cairo with homebrew
  • Create a new venv with python3 and the --system-site-package option i.e. python3 -m venv --system-site-package path/to/env

Check out the comment itself, and let me know if any of this works. There didn't seem to be one clear solution for the issue.


推荐阅读