Kozea / cairocffi

CFFI-based cairo bindings for Python.
https://doc.courtbouillon.org/cairocffi
BSD 3-Clause "New" or "Revised" License
208 stars 54 forks source link

Hack to import constants.py breaks installation via PyInstaller #217

Closed theCapypara closed 1 year ago

theCapypara commented 1 year ago

The 1.6.0 version introduced an import hack, which breaks cairocffi when bundled with PyInstaller and probably also other bundling tools. The reason is this hack: https://github.com/Kozea/cairocffi/blob/master/cairocffi/ffi.py#L18C1-L22

While I'm sure it would be possible to work around this with a PyInstaller hook or similiar, I would consider this odd behavior a bug or something that should at least be documented, I could imagine this breaking in other situations also.

Example error message produced by PyInstaller when trying to start an app consuming cairocffi:

Traceback (most recent call last):
  File "main.py", line 260, in <module>
  File "main.py", line 256, in main
  File "core\async_tasks\delegator.py", line 95, in run_main
  File "main.py", line 237, in run_main
  File "core\modules.py", line 49, in load
  File "core\modules.py", line 75, in _load_windows_modules
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "module\monster\module.py", line 32, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "module\monster\controller\level_up.py", line 27, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "cairosvg\__init__.py", line 26, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "cairosvg\surface.py", line 9, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "cairocffi\__init__.py", line 16, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "cairocffi\ffi.py", line 22, in <module>
  File "<frozen importlib._bootstrap_external>", line 936, in exec_module
  File "<frozen importlib._bootstrap_external>", line 1073, in get_code
  File "<frozen importlib._bootstrap_external>", line 1130, in get_data
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Program Files\\SkyTemple\\cairocffi\\constants.py'

Notably this breaks, because PyInstaller would bundle the python code within the exe executable, and cairocffi now is "hardcoded" to look for the python source file at the given path.

liZe commented 1 year ago

Hi and thanks for the bug report.

We can probably remove this hack now that we don’t use setuptools anymore.

utsiye commented 1 year ago

I have the same problem, is there any way to solve it?

liZe commented 1 year ago

I’ve removed the workaround. Could you try to use the current master branch and tell if it works for you?

utsiye commented 1 year ago

Member

Yes, it worked! I've already rewritten, using a dozen modules, but cairo is the most convenient.

liZe commented 1 year ago

Yes, it worked!

Cool! Let’s release a new version then.