How do I resolve "No module named 'frontend'" error message?

Question:

I have installed PymuPDF/fitz because am trying to extract images from PDF files. However, upon running the code below, I am seeing No module named 'frontend'.

    doc = fitz.open(pdf_path)
            for i in range(len(doc)):
                for img in doc.getPageImageList(i):
                    xref = img[0]
                    pix = fitz.Pixmap(doc, xref)
                    if pix.n < 5:  # this is GRAY or RGB
                        pix.writePNG("p%s-%s.png" % (i, xref))
                    else:  # CMYK: convert to RGB first
                        pix1 = fitz.Pixmap(fitz.csRGB, pix)
                        pix1.writePNG("p%s-%s.png" % (i, xref))
                        pix1 = None
                    pix = None

I have searched but there isn’t single report of this kind of error. I have installed PyMuPDF, muPDF and fitz modules

Here is the error in full:

    Traceback (most recent call last):
      File "/home/waqar/PycharmProjects/predator/ExtractFileImage.py", line 1, in <module>
        import fitz
      File "/home/waqar/anaconda3/envs/retinanet/lib/python3.6/site-packages/fitz/__init__.py", line 1, in <module>
        from frontend import *
    ModuleNotFoundError: No module named 'frontend'
Asked By: Waqar

||

Answers:

In file /home/waqar/anaconda3/envs/retinanet/lib/python3.6/site-packages/fitz/__init__.py

change

from frontend to from fitz.frontend

Answered By: Julien

I’ve solved it by:

pip install PyMuPDF

This will actually allow the import of the fitz you appear to want. (There’s another fitz, which is probably not what you want if you’re manipulating PDF files.)

NOTE: If you get RuntimeError: Directory 'static/' does not exist after install than do:

pip uninstall fitz

for more info see: raise RuntimeError(f"Directory '{directory}' does not exist") RuntimeError: Directory 'static/' does not exist from import fitz

Answered By: Ronin

There is a package named fitz on PyPI. Because PyMuPDF uses the same name, fitz, as its top-level text, both packages cannot co-exist in the same Python – except with the aforementioned change.

Answered By: Jorj McKie

I tried the above solution of pip install PyMuPDF.
But it did not work out of the box.

So, I have used the previous version of PyMuPDF. It worked perfectly for me.

pip install PyMuPDF==1.16.14
Answered By: anil kumar

Calling python on your script should solve the issue:

python script.py

If you don’t use the keyword python, you might get the error.

In my case I was getting:

ModuleNotFoundError: No module named 'fitz'
Answered By: Shayan

You could have used pdfplumber. If the following code returns "None", it’s a scanned pdf otherwise it’s searchable.

with pdfplumber.open(file_name) as pdf:
    page = pdf.pages[0]
    text = page.extract_text()
    print(text)

To extract text from scanned pdf, you can use OCRmyPDF. Easy package

Answered By: Zia

Python3 and you have already installed PyMuPDF module.

pip install --upgrade pip
pip install -U PyMuPDF
Answered By: W. Dan

You should run pip install fitz follow by pip install PyMuPDF. If you have install PyMuPDF, uninstall it and install again.

Answered By: Đạt Quang Trần

This combo works fine for me:

sudo apt install mupdf
sudo apt install libmupdf-dev
pip3 install PyMuPDF==1.16
Answered By: Alexandre Mazel
  1. pip install PyMuPDF
  2. If the above command has the error of fitz.h, then upgrade the python version, I found that python3.6.* will have this problem. If you cannot upgrade the python version, lower the version of pymupdf (version 1.18.0 is OK, pip install PyMuPDF==1.18.0).
Answered By: Yabo Zhao
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.