Skip to content

Import of fitz prints to console instead of generating a DeprecationWarning #5100

Description

@plesubc

Description of the bug

Suppressing deprecation warning does not suppress the deprecation warning.

How to reproduce the bug

import something_up_the_chain_that_uses_fitz

warning: The fitzAPI is deprecated and will be removed in future. Useimport pymupdf instead


While I understand why you have a deprecation warning as of 1.28.2, it appears that your warnings (ie, message and message_warning in __init__.py are sent directly to stdout instead of using warnings and emitting, say a DeprecationWarning . (see __init__py:22307)

The end result of this is that the warning can't be suppressed in the way you might expect. That is, using warnings.filterwarnings("ignore", category=DeprecationWarning, module="fitz") doesn't suppress the output of the message to the console as one would hope. Which also means that any CLI program using PyMuPDF indirectly, will display a (useless or confusing) warning to the end user who now thinks that something is going wrong. And the programmer several levels up has to perform a kludgy workaround like:

sys.stdout = open(os.devnull, 'w')
import something_up_the_chain_that_uses_fitz
sys.stdout = sys.__stdout__

While this works, of course, it's kind of sub-optimal. I realize that the ideal fix is having everyone update their imports but if the import is indirect it's either not possible or involves tracking down the issue in someone else's work. The other option, of course, is to put a ceiling on the PyMuPDF import, which has obvious flaws.

Everything still works, though, so a cosmetic fix may not be high on your priority list.

PyMuPDF version

1.28.2

Operating system

MacOS

Python version

3.13

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions