简体   繁体   中英

How can I disentangle seemingly different imported Python modules under the same version number?

I recently updated PyMuPDF/fitz and so updated my code that uses it to update my use of fitz methods to match the updated naming convention (see PyMuPDF > Deprecated Names ).

Problem: when I call a function I wrote to use fitz's Page.get_text() it sometimes exists as an attribute under the deprecated name and sometimes is in the updated name. Either way it is called, it is imported in the same script, but in one version second script imports the script that imports fitz (via from fitz import fitz ).

How can I gain visibility into where these different sets are attributes are introduced?

Steps taken so far to disentangle and debug: I confirmed the version of PyMuPDF is the same in both situations via this StackOverflow answer with: print(pkg_resources.get_distribution('PyMuPDF').version) => 1.21.0

I checked the attributes of the object with: print(dir(page))

It is a consistent pattern based on which script I use to call my function. #1 w/ getText (deprecated): screenshot showing version number 1.21.0 and getText as an attribute

#2 w/ get_text: screenshot showing version number 1.21.0 and get_text as an attribute

Current solution:

    for page in doc:
        try:
            text += page.get_text()
        except AttributeError:
            text += page.getText()

There are a few ways to handle situations aroung name deprecation:

  1. You can enable old names by executing fitz.restore_aliases() right after import. This will make old names available again (but keep the new ones too).

  2. You can execute a utitlity we have provided. This can be given folders as parameters (or single Python files). It will walk through the source code and replace old names with new ones. You also can request backups of your Python files. Folders will be processed recursively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM