CairoSVG
CairoSVG copied to clipboard
CairoSVG not working on Mac M2
CairoSVG is not working on Mac M2 and gives the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/user_name/Library/Python/3.8/lib/python/site-packages/cairosvg/__init__.py", line 26, in <module>
from . import surface # noqa isort:skip
File "/Users/user_name/Library/Python/3.8/lib/python/site-packages/cairosvg/surface.py", line 9, in <module>
import cairocffi as cairo
File "/Users/user_name/Library/Python/3.8/lib/python/site-packages/cairocffi/__init__.py", line 47, in <module>
cairo = dlopen(
File "/Users/user_name/Library/Python/3.8/lib/python/site-packages/cairocffi/__init__.py", line 44, 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), '/System/Volumes/Preboot/Cryptexes/OSlibcairo.so.2' (no such file), '/usr/lib/libcairo.so.2' (no such file, not in dyld cache), 'libcairo.so.2' (no such file), '/opt/homebrew/opt/cairo/libcairo.so.2' (no such file), '/opt/homebrew/opt/cairo/lib/libcairo.so.2' (no such file), '/libcairo.so.2' (no such file)
cannot load library 'libcairo.2.dylib': dlopen(libcairo.2.dylib, 0x0002): tried: 'libcairo.2.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibcairo.2.dylib' (no such file), '/usr/lib/libcairo.2.dylib' (no such file, not in dyld cache), 'libcairo.2.dylib' (no such file), '/opt/homebrew/opt/cairo/libcairo.2.dylib' (no such file), '/opt/homebrew/opt/cairo/lib/libcairo.2.dylib' (no such file), '/libcairo.2.dylib' (no such file)
cannot load library 'libcairo-2.dll': dlopen(libcairo-2.dll, 0x0002): tried: 'libcairo-2.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibcairo-2.dll' (no such file), '/usr/lib/libcairo-2.dll' (no such file, not in dyld cache), 'libcairo-2.dll' (no such file), '/opt/homebrew/opt/cairo/libcairo-2.dll' (no such file), '/opt/homebrew/opt/cairo/lib/libcairo-2.dll' (no such file), '/libcairo-2.dll' (no such file)
This error occurs when trying to import cairocffi module. Steps to reproduce:
- Install CairoSVG on a Mac M2 using pip install cairosvg.
- Try to import cairosvg module in a Python script or interactive Python shell.
Hi!
If you want to use cairosvg on Mac, you have to install cairo. That’s not a Python library, so you’ll have to find another way than pip to install it. The easiest way is probably to use Homebrew, and then to use Hombrew’s Python to install cairosvg.
This issue has been reported before, a workaround can be found here (works on my M2 Mac, look for the tip submitted by @sinwoobang):
#354
HI @liZe I have installed the cairo and other required packages also. As @varalgit Suggested, I also tried all the solution from #354 thread, but still same issue, The installation is smooth on M1 using following steps
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew update
brew install cairo libffi pkg-config
# activate virtual env
pip install cairosvg
But not able to find any solution for M2, Also tried multiple solution os other Mac M2 thinking that it might be a issue of my pc in particular, but thats not the case, the issue is same on each M2 I have tried it. For now I have shifted to other libraries, but very curious to get cairo working, So let me know if you have anything else
Hi @KalpeshjagannathMahajan,
You have to use Homebrew’s Python to make CairoSVG find Homebrew’s Cairo. Be sure that your virtual environment has been created using Hombrew’s Python.
$ which python3 # should print the path of Hombrew’s Python
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install cairosvg
$ cairosvg --help # should print CairoSVG’s help
Hi @KalpeshjagannathMahajan,
You have to use Homebrew’s Python to make CairoSVG find Homebrew’s Cairo. Be sure that your virtual environment has been created using Hombrew’s Python.
$ which python3 # should print the path of Hombrew’s Python $ python3 -m venv venv $ source venv/bin/activate $ pip install cairosvg $ cairosvg --help # should print CairoSVG’s help
And you should follow the hint from @sinwoobang (https://github.com/Kozea/CairoSVG/issues/354#issuecomment-1160552256):
If nothing helped you, try this workaround. On your project root; where
import cairosvg
sudo ln -s /opt/homebrew/lib/libcairo* .
This is a way to create symbolic links locally.
$ cairosvg --help
Traceback (most recent call last):
File "/Users/kijinchoi/.anyenv/envs/pyenv/versions/3.10.2/bin/cairosvg", line 5, in <module>
from cairosvg.__main__ import main
File "/Users/kijinchoi/.anyenv/envs/pyenv/versions/3.10.2/lib/python3.10/site-packages/cairosvg/__init__.py", line 26, in <module>
from . import surface # noqa isort:skip
File "/Users/kijinchoi/.anyenv/envs/pyenv/versions/3.10.2/lib/python3.10/site-packages/cairosvg/surface.py", line 9, in <module>
import cairocffi as cairo
File "/Users/kijinchoi/.anyenv/envs/pyenv/versions/3.10.2/lib/python3.10/site-packages/cairocffi/__init__.py", line 47, in <module>
cairo = dlopen(
File "/Users/kijinchoi/.anyenv/envs/pyenv/versions/3.10.2/lib/python3.10/site-packages/cairocffi/__init__.py", line 44, in dlopen
raise OSError(error_message) # pragma: no cover
OSError: no library called "cairo-2" was found
no library called "libcairo-2" was found
cannot load library '/usr/local/lib/libcairo.dylib': dlopen(/usr/local/lib/libcairo.dylib, 0x0002): tried: '/usr/local/lib/libcairo.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/System/Volumes/Preboot/Cryptexes/OS/usr/local/lib/libcairo.dylib' (no such file), '/usr/local/lib/libcairo.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/opt/homebrew/Cellar/cairo/1.16.0_5/lib/libcairo.2.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Cellar/cairo/1.16.0_5/lib/libcairo.2.dylib' (no such file), '/opt/homebrew/Cellar/cairo/1.16.0_5/lib/libcairo.2.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64'))
cannot load library 'libcairo.so.2': dlopen(libcairo.so.2, 0x0002): tried: 'libcairo.so.2' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibcairo.so.2' (no such file), '/usr/lib/libcairo.so.2' (no such file, not in dyld cache), 'libcairo.so.2' (no such file), '/usr/local/lib/libcairo.so.2' (no such file), '/usr/lib/libcairo.so.2' (no such file, not in dyld cache). Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo.so.2'
cannot load library '/usr/local/lib/libcairo.2.dylib': dlopen(/usr/local/lib/libcairo.2.dylib, 0x0002): tried: '/usr/local/lib/libcairo.2.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/System/Volumes/Preboot/Cryptexes/OS/usr/local/lib/libcairo.2.dylib' (no such file), '/usr/local/lib/libcairo.2.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/opt/homebrew/Cellar/cairo/1.16.0_5/lib/libcairo.2.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Cellar/cairo/1.16.0_5/lib/libcairo.2.dylib' (no such file), '/opt/homebrew/Cellar/cairo/1.16.0_5/lib/libcairo.2.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64'))
cannot load library 'libcairo-2.dll': dlopen(libcairo-2.dll, 0x0002): tried: 'libcairo-2.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibcairo-2.dll' (no such file), '/usr/lib/libcairo-2.dll' (no such file, not in dyld cache), 'libcairo-2.dll' (no such file), '/usr/local/lib/libcairo-2.dll' (no such file), '/usr/lib/libcairo-2.dll' (no such file, not in dyld cache). Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo-2.dll'
mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')
Same here, but mine is saying there is a match file but not x86_64.
@unclok Hello, have you tried installing cairosvg through homebrew for the recent Mac Chips?
/usr/local/lib
- I found it on your debug log - is the classic Mac homebrew's installation path, have you mixed Rosetta and vanilla M2?
@sinwoobang Sorry for the late reply. I've installed cairosvg by pip3 that time. After #354 , I found out that my cairosvg is working fine on my Mac M2, so I don't need to try any other way to install right now. But thank you for mentioning alternative ways!
usage: cairosvg [-h] [-v] [-f {eps,pdf,png,ps,svg}] [-d DPI] [-W WIDTH] [-H HEIGHT] [-s SCALE] [-b COLOR] [-n] [-i] [-u] [--output-width OUTPUT_WIDTH] [--output-height OUTPUT_HEIGHT]
[-o OUTPUT]
input
Convert SVG files to other formats
positional arguments:
input input filename or URL
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-f {eps,pdf,png,ps,svg}, --format {eps,pdf,png,ps,svg}
output format
-d DPI, --dpi DPI ratio between 1 inch and 1 pixel
-W WIDTH, --width WIDTH
width of the parent container in pixels
-H HEIGHT, --height HEIGHT
height of the parent container in pixels
-s SCALE, --scale SCALE
output scaling factor
-b COLOR, --background COLOR
output background color
-n, --negate-colors replace every vector color with its complement
-i, --invert-images replace every raster pixel with its complementary color
-u, --unsafe fetch external files, resolve XML entities and allow very large files (WARNING: vulnerable to XXE attacks and various DoS)
--output-width OUTPUT_WIDTH
desired output width in pixels
--output-height OUTPUT_HEIGHT
desired output height in pixels
-o OUTPUT, --output OUTPUT
output filename