11

I wanted to import fitz in my code. To do that, I tried installing PyMuPDF using

pip3 install PyMuPDF

However, this installation fails and returns this error:

fitz/fitz_wrap.c:2754:10: fatal error: 'fitz.h' file not found
#include <fitz.h>
         ^~~~~~~~
1 error generated.
error: command '/opt/homebrew/clang' failed with exit code 1

I also tried installing mupdf and mupdf-tools via Homebrew. None of them could fix this issue. I would appreciate any help to fix this installation error!

vy32
  • 28,461
  • 37
  • 122
  • 246
Nirali
  • 253
  • 3
  • 9

2 Answers2

31

Assuming you are using M1 Mac, you can solve this problem installing mupdf, swig, freetype and latest version of PyMuPDF:

brew install mupdf swig freetype
pip install https://github.com/pymupdf/PyMuPDF/archive/master.tar.gz

Source: https://github.com/pymupdf/PyMuPDF/discussions/875

Juan García
  • 1,690
  • 1
  • 15
  • 17
  • Wow, this worked like a charm! I tried so many solutions posted online but nothing else worked except this one. Thank you so much! – Nirali Aug 06 '21 at 04:24
  • Excellent! After the `brew install mupdf swig freetype`, doing `pip install --upgrade pymupdf` also worked well. – fedorqui Nov 19 '21 at 13:06
  • You save my day, works... thanks – Alex Dec 01 '21 at 02:25
  • I had to install `swig-python` as well: `sudo port install swig-python`, only after that the pip install worked – kabr Sep 07 '22 at 08:48
7

The instructions provided by Juan Garcia worked until June 18th 2022 for me. There's a few workarounds.

Solution #1

Install the wheel package using pip and swig + freetype using homebrew. PyMuPDF should build itself successfully when running pip install pymupdf. This takes about five minutes on a late model M1 MBP. This does not require the mupdf package installed via homebrew.

Solution #2

If you're using homebrew you can downgrade to 1.19.x with the following:

cd "$(brew --repo homebrew/core)" && git checkout 457cb59a52ff7666dc146c1c480d2ea98d63564f;

# If you already have mupdf installed use `brew reinstall` instead of `brew install`
cd "$(brew --repo homebrew/core)" && HOMEBREW_NO_AUTO_UPDATE=1 brew install mupdf;
cd "$(brew --repo homebrew/core)" && git checkout master && brew pin mupdf;

brew install swig freetype;

pip install https://github.com/pymupdf/PyMuPDF/archive/refs/tags/1.19.6.tar.gz;

For reference here is the Formula commit history: https://github.com/Homebrew/homebrew-core/commits/master/Formula/mupdf.rb

Aea
  • 976
  • 8
  • 10