Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pip installation of cairocffi not working on mac #28

Closed
nitinbodke opened this issue Feb 1, 2014 · 24 comments
Closed

pip installation of cairocffi not working on mac #28

nitinbodke opened this issue Feb 1, 2014 · 24 comments

Comments

@nitinbodke
Copy link

I tried to install cairocffi on Mac.
I am getting error while importing cairocffi.

Below are environment details:
Model: MacBook Pro
OS X: 10.8.5
Python: 2.7.2

Command Log:

sudo pip install cairocffi

...some warnings ....

    35 warnings generated.
    clang -bundle -undefined dynamic_lookup -Wl,-F. -arch i386 -arch x86_64 build/temp.macosx-10.8-intel-2.7/c/_cffi_backend.o -L/opt/local/lib -lffi -o build/lib.macosx-10.8-intel-2.7/_cffi_backend.so
    ld: warning: ignoring file /opt/local/lib/libffi.dylib, file was built for x86_64 which is not the architecture being linked (i386): /opt/local/lib/libffi.dylib

  Running setup.py install for pycparser

Successfully installed cairocffi cffi pycparser
Cleaning up...
SymMacToolkit-C02K824VF1G3:specs nitin_bodke$ python
Python 2.7.2 (default, Oct 11 2012, 20:14:37) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cairocffi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/cairocffi/__init__.py", line 39, in <module>
    cairo = dlopen(ffi, 'libcairo.so.2', 'libcairo-2.dll', 'cairo', 'libcairo-2')
  File "/Library/Python/2.7/site-packages/cairocffi/__init__.py", line 34, in dlopen
    return ffi.dlopen(names[0])  # pragma: no cover
  File "/Library/Python/2.7/site-packages/cffi/api.py", line 117, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "/Library/Python/2.7/site-packages/cffi/api.py", line 399, in _make_ffi_library
    backendlib = backend.load_library(name, flags)
OSError: cannot load library libcairo.so.2: dlopen(libcairo.so.2, 2): image not found
>>> 
</snip>
@SimonSapin
Copy link
Member

Hi. Your probably need to install cairo separately. You can get it with Homebrew or Macports. After that, you may need to enable something so that the library can be loaded dynamically. Unfortunately I don’t have access to OS X myself to test this.

@nitinbodke
Copy link
Author

Hi,

I have installed cairo from macports.
This comes with libcairo.2.dylib and not libcairo.so.2.
Here is the extract of the "port contents cairo"

/opt/local/lib/libcairo.2.dylib
/opt/local/lib/libcairo.a
/opt/local/lib/libcairo.dylib
/opt/local/lib/libcairo.la

Regards,
Nitin.

@SimonSapin
Copy link
Member

As you can see with dlopen(ffi, 'libcairo.so.2', 'libcairo-2.dll', 'cairo', 'libcairo-2') in the traceback, cairocffi tries a bunch of different names. Normally, just giving cairo to dlopen should do the trick on OS X.

Does it help to add /opt/local/lib to the LD_LIBRARY_PATH or DYLD_LIBRARY_PATH environment variable?

@nitinbodke
Copy link
Author

Hi,

I have added 'libcairo.2.dylib' to dlopen call and that fixed the issue.

cairo = dlopen(ffi, 'libcairo.so.2', 'libcairo-2.dll', 'cairo', 'libcairo-2', 'libcairo.2.dylib')

python Python 2.7.2 (default, Oct 11 2012, 20:14:37) [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin Type "help", "copyright", "credits" or "license" for more information.

import cairocffi as cairo
quit()

Thanks for help.

Regards,
Nitin.

@SimonSapin
Copy link
Member

Could you try these and tell me which of them work?

import cffi; cffi.FFI().dlopen('cairo')
import cffi; cffi.FFI().dlopen('cairo.2')
import cffi; cffi.FFI().dlopen('libcairo')
import cffi; cffi.FFI().dlopen('libcairo.2')
import cffi; cffi.FFI().dlopen('libcairo.2.dylib')
import cffi; cffi.FFI().dlopen('cairo.2.dylib')
import cffi; cffi.FFI().dlopen('cairo.dylib')
import cffi; cffi.FFI().dlopen('libcairo.dylib')

@nitinbodke
Copy link
Author

libcairo.2.dylib and libcairo.dylib worked for me.
Below is the detailed errors:

(graphite)$ python
Python 2.7.2 (default, Oct 11 2012, 20:14:37) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cffi; cffi.FFI().dlopen('cairo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/graphite/lib/python2.7/site-packages/cffi/api.py", line 117, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "/opt/graphite/lib/python2.7/site-packages/cffi/api.py", line 398, in _make_ffi_library
    raise OSError("library not found: %r" % (name,))
OSError: library not found: 'cairo'
>>> import cffi; cffi.FFI().dlopen('cairo.2')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/graphite/lib/python2.7/site-packages/cffi/api.py", line 117, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "/opt/graphite/lib/python2.7/site-packages/cffi/api.py", line 399, in _make_ffi_library
    backendlib = backend.load_library(name, flags)
OSError: cannot load library cairo.2: dlopen(cairo.2, 2): image not found
>>> import cffi; cffi.FFI().dlopen('libcairo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/graphite/lib/python2.7/site-packages/cffi/api.py", line 117, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "/opt/graphite/lib/python2.7/site-packages/cffi/api.py", line 398, in _make_ffi_library
    raise OSError("library not found: %r" % (name,))
OSError: library not found: 'libcairo'
>>> import cffi; cffi.FFI().dlopen('libcairo.2')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/graphite/lib/python2.7/site-packages/cffi/api.py", line 117, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "/opt/graphite/lib/python2.7/site-packages/cffi/api.py", line 399, in _make_ffi_library
    backendlib = backend.load_library(name, flags)
OSError: cannot load library libcairo.2: dlopen(libcairo.2, 2): image not found
>>> import cffi; cffi.FFI().dlopen('libcairo.2.dylib')
<cffi.api.FFILibrary_libcairo.2.dylib object at 0x104848dd0>
>>> import cffi; cffi.FFI().dlopen('cairo.2.dylib')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/graphite/lib/python2.7/site-packages/cffi/api.py", line 117, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "/opt/graphite/lib/python2.7/site-packages/cffi/api.py", line 399, in _make_ffi_library
    backendlib = backend.load_library(name, flags)
OSError: cannot load library cairo.2.dylib: dlopen(cairo.2.dylib, 2): image not found
>>> import cffi; cffi.FFI().dlopen('cairo.dylib')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/graphite/lib/python2.7/site-packages/cffi/api.py", line 117, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "/opt/graphite/lib/python2.7/site-packages/cffi/api.py", line 399, in _make_ffi_library
    backendlib = backend.load_library(name, flags)
OSError: cannot load library cairo.dylib: dlopen(cairo.dylib, 2): image not found
>>> import cffi; cffi.FFI().dlopen('libcairo.dylib')
<cffi.api.FFILibrary_libcairo.dylib object at 0x10485b450>
>>> 

@SimonSapin
Copy link
Member

By the way, this

```
code
```

works nicer on GitHub than <snip> and </snip>

@SimonSapin
Copy link
Member

Ok, I just pushed 01681e6. Let me know if it’s still broken or if you need this on PyPI.

@nitinbodke
Copy link
Author

Sorry, this is the first time I am using GitHub commenting.

To give the bigger picture, I am trying to install Graphite, I stuck upon cairo installation. I came across "http://stackoverflow.com/questions/11491268/install-pycairo-in-virtualenv"
and used your solution.

I am hitting following traceback.
At this point, I don't know whether this is issue with cairocffi.
If this is off topic, please accept my apologies.

"Traceback (most recent call last):
  File "/opt/graphite/lib/python2.7/site-packages/django/core/handlers/base.py", line 103, in get_response
    resolver_match = resolver.resolve(request.path_info)
  File "/opt/graphite/lib/python2.7/site-packages/django/core/urlresolvers.py", line 321, in resolve
    sub_match = pattern.resolve(new_path)
  File "/opt/graphite/lib/python2.7/site-packages/django/core/urlresolvers.py", line 321, in resolve
    sub_match = pattern.resolve(new_path)
  File "/opt/graphite/lib/python2.7/site-packages/django/core/urlresolvers.py", line 223, in resolve
    return ResolverMatch(self.callback, args, kwargs, self.name)
  File "/opt/graphite/lib/python2.7/site-packages/django/core/urlresolvers.py", line 230, in callback
    self._callback = get_callable(self._callback_str)
  File "/opt/graphite/lib/python2.7/site-packages/django/utils/functional.py", line 31, in wrapper
    result = func(*args)
  File "/opt/graphite/lib/python2.7/site-packages/django/core/urlresolvers.py", line 97, in get_callable
    mod = import_module(mod_name)
  File "/opt/graphite/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/opt/graphite/webapp/graphite/render/views.py", line 36, in <module>
    from graphite.render.evaluator import evaluateTarget
  File "/opt/graphite/webapp/graphite/render/evaluator.py", line 47, in <module>
    from graphite.render.functions import SeriesFunctions
  File "/opt/graphite/webapp/graphite/render/functions.py", line 33, in <module>
    from graphite.render.glyph import format_units
  File "/opt/graphite/webapp/graphite/render/glyph.py", line 15, in <module>
    import cairocffi as cairo
  File "/opt/graphite/lib/python2.7/site-packages/cairocffi/__init__.py", line 16, in <module>
    from . import constants
ImportError: cannot import name constants"

@nitinbodke
Copy link
Author

I think there are couple of places we need to modify to fix this.
Below are the error when I am not using virtual env.

">>> import cairocffi as cairo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/cairocffi/__init__.py", line 39, in <module>
    cairo = dlopen(ffi, 'libcairo.so.2', 'libcairo-2.dll', 'cairo', 'libcairo-2')
  File "/Library/Python/2.7/site-packages/cairocffi/__init__.py", line 34, in dlopen
    return ffi.dlopen(names[0])  # pragma: no cover
  File "/Library/Python/2.7/site-packages/cffi/api.py", line 117, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "/Library/Python/2.7/site-packages/cffi/api.py", line 399, in _make_ffi_library
    backendlib = backend.load_library(name, flags)
OSError: cannot load library libcairo.so.2: dlopen(libcairo.so.2, 2): image not found
>>> 

@SimonSapin
Copy link
Member

Please open another issue with this traceback and some context such as steps to reproduce it.

@nitinbodke
Copy link
Author

Sure, I will open separate issue.

@SimonSapin
Copy link
Member

Closing this as it seems solved. Let me know if you need it on PyPI.

@colevscode
Copy link

Is this fix reflected on PyPI? I received this error recently. Currently have v 0.5.2 installed according to pip freeze.

@SimonSapin
Copy link
Member

@colevscode Looks like it’s not. Can you confirm that this change actually fixes the issue for you? Install from git with:

pip uninstall cairocffi  # convince pip to actually do the next step despite identical version numbers
pip install git+https://github.com/SimonSapin/cairocffi.git

@czpython
Copy link

I'm also getting this error. Tried all of the above ways to import and still got errors. Also used latest from master.

@SimonSapin
Copy link
Member

@czpython Please copy the exact error message you get.

@czpython
Copy link

so brew unlink cairo && brew install cairo seems to have worked. guess I was running old cairo version.

@khink
Copy link

khink commented Feb 18, 2015

I have this problem too. In my Plone project, we have WeasyPrint as a dependency, which in its turn depends on cairo.

Platform:
OSX Yosemite 10.10.2
cairo installed with Mac ports
Python 2.6
cairocffi 0.5.3 (error occurs also on 0.5.4 and 6.0)
WeasyPrint 0.21 (error occurs also 0.22 and 0.23)
cffi 0.8.2 (error occurs also on 0.8.6)
It's a zc.buildout environment, with a virtualenv'ed Python.

Contents of /opt/local/lib/libcairo.*

-rwxr-xr-x  1 root  admin   965496 Dec  2 01:55 /opt/local/lib/libcairo.2.dylib
-rw-r--r--  1 root  admin  1722640 Dec  2 01:55 /opt/local/lib/libcairo.a
lrwxr-xr-x  1 root  admin       16 Dec  2 01:55 /opt/local/lib/libcairo.dylib -> libcairo.2.dylib

The error i get when the instance starts up:

OSError: cannot load library libcairo.so.2: dlopen(libcairo.so.2, 2): image not found

I can reproduce this in my buildout's Python interpreter by doing:

$ ./bin/zopepy
>>> import cairocffi as cairo
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "build/bdist.macosx-10.10-intel/egg/cairocffi/__init__.py", line 40, in <module>
  File "build/bdist.macosx-10.10-intel/egg/cairocffi/__init__.py", line 34, in dlopen
  File "/Users/kees/.buildout/eggs/cffi-0.8.2-py2.6-macosx-10.10-intel.egg/cffi/api.py", line 119, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "/Users/kees/.buildout/eggs/cffi-0.8.2-py2.6-macosx-10.10-intel.egg/cffi/api.py", line 412, in _make_ffi_library
    backendlib = _load_backend_lib(backend, libname, flags)
  File "/Users/kees/.buildout/eggs/cffi-0.8.2-py2.6-macosx-10.10-intel.egg/cffi/api.py", line 401, in _load_backend_lib
    return backend.load_library(name, flags)
OSError: cannot load library libcairo.so.2: dlopen(libcairo.so.2, 2): image not found

The change you mentioned (01681e6) should be in 0.6 right? I also tried that, and got the same error.

@khink
Copy link

khink commented Feb 18, 2015

... but

export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib

fixed it for me. Thanks!

@curtisforrester
Copy link

+1 on the export, khink. Since I've installed Yosemite on this MacbookPro, I've started over: I created a new virtualenv and installed the dependencies I need for our Django app. We use WeasyPrint, so I installed the dependencies it needs using Mac Port. But I still have to add that env variable to load.

@SebaRGFSL
Copy link

Hello there, i'm trying to use this library on a django project installing it using pipenv install -r requirements.txt
where in that file it appears as a dependency of WeasyPrint~=52.2
Installation runs succesfully but at the time of running the server with pipenv run python3 manage.py runserver i get the following output:

Traceback (most recent call last):
  File "/Users/sebita/Desktop/SymplPay/src/manage.py", line 22, in <module>
    main()
  File "/Users/sebita/Desktop/SymplPay/src/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/sebita/.local/share/virtualenvs/SymplPay-Fr0ykp7r/lib/python3.10/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/Users/sebita/.local/share/virtualenvs/SymplPay-Fr0ykp7r/lib/python3.10/site-packages/django/core/management/__init__.py", line 377, in execute
    django.setup()
  File "/Users/sebita/.local/share/virtualenvs/SymplPay-Fr0ykp7r/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/sebita/.local/share/virtualenvs/SymplPay-Fr0ykp7r/lib/python3.10/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/Users/sebita/.local/share/virtualenvs/SymplPay-Fr0ykp7r/lib/python3.10/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/sebita/Desktop/SymplPay/src/apps/payments/models.py", line 19, in <module>
    import weasyprint
  File "/Users/sebita/.local/share/virtualenvs/SymplPay-Fr0ykp7r/lib/python3.10/site-packages/weasyprint/__init__.py", line 469, in <module>
    from .css import preprocess_stylesheet  # noqa isort:skip
  File "/Users/sebita/.local/share/virtualenvs/SymplPay-Fr0ykp7r/lib/python3.10/site-packages/weasyprint/css/__init__.py", line 27, in <module>
    from . import computed_values, counters, media_queries
  File "/Users/sebita/.local/share/virtualenvs/SymplPay-Fr0ykp7r/lib/python3.10/site-packages/weasyprint/css/computed_values.py", line 15, in <module>
    from .. import text
  File "/Users/sebita/.local/share/virtualenvs/SymplPay-Fr0ykp7r/lib/python3.10/site-packages/weasyprint/text.py", line 11, in <module>
    import cairocffi as cairo
  File "/Users/sebita/.local/share/virtualenvs/SymplPay-Fr0ykp7r/lib/python3.10/site-packages/cairocffi/__init__.py", line 48, in <module>
    cairo = dlopen(
  File "/Users/sebita/.local/share/virtualenvs/SymplPay-Fr0ykp7r/lib/python3.10/site-packages/cairocffi/__init__.py", line 45, in dlopen
    raise OSError(error_message)  # pragma: no cover
OSError: no library called "cairo-2" was found
no library called "cairo" was found
no library called "libcairo-2" was found
cannot load library 'libcairo.so.2': dlopen(libcairo.so.2, 0x0002): tried: 'libcairo.so.2' (no such file), '/usr/lib/libcairo.so.2' (no such file), '/Users/sebita/Desktop/SymplPay/libcairo.so.2' (no such file)
cannot load library 'libcairo.2.dylib': dlopen(libcairo.2.dylib, 0x0002): tried: 'libcairo.2.dylib' (no such file), '/usr/lib/libcairo.2.dylib' (no such file), '/Users/sebita/Desktop/SymplPay/libcairo.2.dylib' (no such file)
cannot load library 'libcairo-2.dll': dlopen(libcairo-2.dll, 0x0002): tried: 'libcairo-2.dll' (no such file), '/usr/lib/libcairo-2.dll' (no such file), '/Users/sebita/Desktop/SymplPay/libcairo-2.dll' (no such file)

I'm using a MacOS Monterrey 12.4 with an M1 Pro

any suggestions on why this isn't working? Thanks in advance

@Eimisas
Copy link

Eimisas commented Jul 4, 2022

@SebaRGFSL

The following export fixed the issue for me:

export DYLD_FALLBACK_LIBRARY_PATH=/opt/homebrew/lib

Make sure you have libcairo dependencies within homebrew location (or change it accordingly depending on your setup)

@Jacob220728
Copy link

Do u fixed?how?plz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants