LoginSignup
1
1

More than 1 year has passed since last update.

(PyMuPDF) M1 Mac で "import fitz"できなかったがキャッシュ削除で解決した(備忘録)

Last updated at Posted at 2022-09-26

"import fitz”で問題が発生しました (PyMuPDF)

の”import fitz"を実行できませんでしたが、キャッシュ削除で解決しました。
解決作業の備忘録です。

手順(以下の作業は、arm64上で行なっています)

name -m 

#arm64

arm64と返ってきたなら、以下を実行

そうでない場合は、Rosettaをoffにしてやる必要があります。

以下を実行

pip3 install fitz
brew install mupdf swig freetype
pip3 install PyMuPDF
pip3 install frontend

下記サイトの方法をpip3に変えるだけでは、

pip3 install fitz
brew install mupdf swig freetype
pip3 install https://github.com/pymupdf/PyMuPDF/archive/master.tar.gz
# ERROR: You must give at least one requirement to install (see "pip help install")
pip3 install frontend

と、エラーになります。ちなみに、

pip3 install https://github.com/pymupdf/PyMuPDF/archive/master.tar.gz
# ERROR: You must give at least one requirement to install (see "pip help install")

で、インストールしたPyMuPDFは、

pip3 install PyMuPDF

で削除できます(そもそもインストールできていなかったかもしれませんが、時間の都合で検証できていません...)。

発生したエラーとその解決法

インストールコマンドの実行中、もしくはプログラム実行時にエラーが出る場合、以前利用したpip3やbrewのキャッシュが残っていることがの場合もあるようです。今回発生していた"import fitz"のエラーは、こちらが原因だったようでした。

pip3 cache purge

brewが原因なら、

を参考にして、brew install mupdf swig freetypeでインストールしたものを削除して

brew cleanup -s

今回は、以下の手順で解決しました。

pip3 uninstall fitz
pip3 uninstall PyMuPDF
pip3 uninstall frontend
pip3 cache purge
brew cleanup -s
# ここまで既存パッケージの削除
# ここから再インストール
pip3 install fitz
# 以下のbrew installの行は、不要かもしれません
brew install mupdf swig freetype
# 以下は、必ず必要なはず
pip3 install PyMuPDF
pip3 install frontend

無事、PyMuPDFを利用して、PythonでPDFを読み込みテキストを抽出できるようになりました。

感想

試行錯誤しても、なかなかエラーが取れなかったので解決できてホッとしました。
原因がわからない時にログを読むのは大変でしたが、読みが当たって解決するとやった甲斐を感じますね!

その他のエラーとその解決法

その他、参考

    raise RuntimeError(f"Directory '{directory}' does not exist")
RuntimeError: Directory 'static/' does not exist

で、怒られた場合は

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1