Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name 'utils' from partially initialized module 'pymupdf' (most likely due to a circular import) #2653

Closed
apyrgio opened this issue Oct 29, 2024 · 10 comments
Milestone

Comments

@apyrgio
Copy link

apyrgio commented Oct 29, 2024

Describe the bug
cx-Freeze cannot import the latest iteration of PyMuPDF (1.24.11 or greater).

To Reproduce
Install PyMuPDF in a virtual environment, and use cx-Freeze to create a bundle. The resulting .exe file cannot import PyMuPDf:

Traceback (most recent call last):
  File "C:\Users\juhok\AppData\Local\pypoetry\Cache\virtualenvs\dangerzone-_8ncJGJf-py3.12\Lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 140, in run
    module_init.run(f"__main__{name}")
  File "C:\Users\juhok\AppData\Local\pypoetry\Cache\virtualenvs\dangerzone-_8ncJGJf-py3.12\Lib\site-packages\cx_Freeze\initscripts\console.py", line 25, in run
    exec(code, main_globals)
  File "install\windows\dangerzone-cli.py", line 1, in <module>
  File "C:\GitHub\dangerzone\dangerzone\__init__.py", line 27, in <module>
    from .cli import cli_main as main
  File "C:\GitHub\dangerzone\dangerzone\cli.py", line 10, in <module>
    from .isolation_provider.container import Container
  File "C:\GitHub\dangerzone\dangerzone\isolation_provider\container.py", line 12, in <module>
    from .base import IsolationProvider, terminate_process_group
  File "C:\GitHub\dangerzone\dangerzone\isolation_provider\base.py", line 12, in <module>
    import fitz
  File "C:\Users\juhok\AppData\Local\pypoetry\Cache\virtualenvs\dangerzone-_8ncJGJf-py3.12\Lib\site-packages\fitz\__init__.py", line 2, in <module>
    from pymupdf import *
  File "C:\Users\juhok\AppData\Local\pypoetry\Cache\virtualenvs\dangerzone-_8ncJGJf-py3.12\Lib\site-packages\pymupdf\__init__.py", line 21706, in <module>
    from . import utils
ImportError: cannot import name 'utils' from partially initialized module 'pymupdf' (most likely due to a circular import) (C \GitHub\dangerzone\build\exe.win-amd64-3.12\lib\pymupdf\__init__.pyc)

(taken from a user issue in the Dangerzone repo freedomofpress/dangerzone#974)

Expected behavior
PyMuPDF should be imported.

Screenshots

Desktop (please complete the following information):

  • Windows
  • OS architecture (e.g. amd64): x86_64
  • cx_Freeze version [e.g. 6.11]: 7.2.3
  • Python version [e.g. 3.10]: 3.12

Additional context
A similar issue was previously reported here (#2497) but the error is different this time. I believe the reason is that PyMuPDF underwent some architectural changes in the meantime.

Also, it's worth noting that the bundle works, if I explicitly specify the missing module, e.g., with packages = [..., "pymupdf.utils"]. I guess we need to load this module in the PyMuPDF hook, but it's not super clean.

@marcelotduarte
Copy link
Owner

You can test with:
pip install --no-cache --pre git+https://github.com/marcelotduarte/cx_Freeze.git --force
If using uv:
uv pip install --no-cache --pre git+https://github.com/marcelotduarte/cx_Freeze.git --force-reinstall

@apyrgio
Copy link
Author

apyrgio commented Oct 30, 2024

Thanks a bunch for the quick fix! I'll give it a test once I hop in my Windows machine.

@apyrgio
Copy link
Author

apyrgio commented Oct 31, 2024

Hm, I'm getting the following error when I try to run cx-Freeze from Git:

distutils.errors.DistutilsOptionError: no base named 'Win32GUI' ('Win32GUI-cpython-312-win_amd64')

Maybe I'm missing something though. I'll try to figure it out and get back to you.

@marcelotduarte
Copy link
Owner

You can get the wheel from this link.

@apyrgio
Copy link
Author

apyrgio commented Nov 4, 2024

Ok, I just used that wheel, but now I'm seeing a different error:

Traceback (most recent call last):
  File "C:\Users\apyrg\AppData\Local\pypoetry\Cache\virtualenvs\dangerzone-kK8ZKSCU-py3.12\Lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 119, in  run
    module_init.run(f"__main__{name}")
  File "C:\Users\apyrg\AppData\Local\pypoetry\Cache\virtualenvs\dangerzone-kK8ZKSCU-py3.12\Lib\site-packages\cx_Freeze\initscripts\console.py", line 25, in run
    exec(code, main_globals)
  File "install\windows\dangerzone-cli.py", line 1, in <module>
    import dangerzone
  File "C:\Users\apyrg\playground\dangerzone\dangerzone\__init__.py", line 27, in <module>
    from .cli import cli_main as main
  File "C:\Users\apyrg\playground\dangerzone\dangerzone\cli.py", line 5, in <module>
    import click
  File "C:\Users\apyrg\AppData\Local\pypoetry\Cache\virtualenvs\dangerzone-kK8ZKSCU-py3.12\Lib\site-packages\click\__init__.py", line 7, in <module>
    from .core import Argument as Argument
  File "C:\Users\apyrg\AppData\Local\pypoetry\Cache\virtualenvs\dangerzone-kK8ZKSCU-py3.12\Lib\site-packages\click\core.py", line 11, in <module>
    from gettext import gettext as _
ModuleNotFoundError: No module named 'gettext'

This errors happens even if I do the packages = [..., "pymupdf.utils"] hack. It looks like something is broken in this wheel, and now it can't package click.

@marcelotduarte
Copy link
Owner

Ya. Try with the most recent.

@marcelotduarte
Copy link
Owner

@apyrgio Can you test and give feedback?

@apyrgio
Copy link
Author

apyrgio commented Nov 8, 2024

Sorry for the delay, it's just that I haven't managed to switch to my Windows machine yet. I'll let you know the soonest, most likely on Monday.

@marcelotduarte marcelotduarte modified the milestones: 7.3.0, 7.2.5 Nov 11, 2024
@apyrgio
Copy link
Author

apyrgio commented Nov 11, 2024

Just managed to give it a whirl, and it works!

@marcelotduarte
Copy link
Owner

Release 7.2.5 is out!
Documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants