wand.image Import Error: shared library not found. You probably had not installed ImageMagick library

1k views Asked by At

I am attempting to use the PDFPlumber library, which uses Wand's image format. However, upon trying to run:

from wand.image import Image

I get this error:

Traceback (most recent call last):
 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/wand/api.py", line 151, in <module>
libraries = load_library()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/wand/api.py", line 140, in load_library
raise IOError('cannot find library; tried paths: ' + repr(tried_paths))
OSError: cannot find library; tried paths: ['/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWandHDRI.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWandHDRI-2.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-7.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-7HDRI.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-7HDRI-2.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-7.Q8.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-7.Q8HDRI.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-7.Q8HDRI-2.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-7.Q16.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-7.Q16HDRI.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-7.Q16HDRI-2.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-6.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-6HDRI.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-6HDRI-2.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-Q16.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-Q16HDRI.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-Q16HDRI-2.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-Q8.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-Q8HDRI.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-Q8HDRI-2.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-6.Q16.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-6.Q16HDRI.dylib', '/opt/homebrew/opt/imagemagick@6/lib/lib/libMagickWand-6.Q16HDRI-2.dylib']

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/wand/api.py", line 177, in <module>
'Try to install:\n  ' + msg)
ImportError: MagickWand shared library not found.
You probably had not installed ImageMagick library.
Try to install:
    brew install freetype imagemagick

I first tried installing normally:

pip3 install wand
brew install imagemagick

Then, I tried using the method listed here, and tried the following:

pip3 install wand
brew uninstall imagemagick
brew install imagemagick@6
brew unlink imagemagick && brew link imagemagick@6

export MAGICK_HOME="/opt/homebrew/opt/imagemagick@6/"
export PATH="/opt/homebrew/opt/imagemagick@6/bin:$PATH"

but am still getting the same error.

I also tried the solutions listed here and confirmed that I am running 64-bit python 3.7 as mentioned here. How can I fix this? I'm especially confused because after running:

cd /opt/homebrew/opt/imagemagick@6/lib
ls

I can see that /opt/homebrew/opt/imagemagick@6/lib/libMagickWand-6.Q16.dylib is where Wand expects it to be (listed in the tried paths in the error above):

 ImageMagick                     libMagickCore-6.Q16.7.dylib     libMagickWand-6.Q16.a
libMagick++-6.Q16.9.dylib       libMagickCore-6.Q16.a           libMagickWand-6.Q16.dylib
libMagick++-6.Q16.a             libMagickCore-6.Q16.dylib       libMagickWand-6.Q16.la
libMagick++-6.Q16.dylib         libMagickCore-6.Q16.la          pkgconfig
libMagick++-6.Q16.la            libMagickWand-6.Q16.7.dylib
2

There are 2 answers

1
Calster On

I faced the same issue when tried running Wand on M1 mac, even though the same steps worked on x86 system. Solution that worked for me is to install ImageMagick via brew in x86 mode:

alias brew86="arch -x86_64 /usr/local/bin/brew"
brew86 install imagemagick

# get imagemagick installation path
brew86 info imagemagick
export MAGICK_HOME=/usr/local/Cellar/imagemagick/7.1.0-49_1 
export PATH="$MAGICK_HOME/bin:$PATH"
1
Asif Khan On

I also face same issue on Mac M1 machine. I only set the ENV variables like this after checking brew info imagemagick command. There is no need to reinstall imagemigick as arch -x86_64 at least for me on Mac M1 OS Monterey 12.6

export MAGICK_HOME=/opt/homebrew/Cellar/imagemagick/7.1.0-51 export PATH="$MAGICK_HOME/bin:$PATH"

and it working fine.