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

Fix root when frozen with pyinstaller #540

Merged
merged 1 commit into from
Nov 16, 2017
Merged

Fix root when frozen with pyinstaller #540

merged 1 commit into from
Nov 16, 2017

Conversation

johnnymetz
Copy link
Contributor

When freezing a WeasyPrint application with PyInstaller, root needs to be set to sys._MEIPASS, which is the absolute path to the bundle folder (source:
https://pythonhosted.org/PyInstaller/runtime-information.html). _MEIPASS appears to be uniquely set by PyInstaller so my code change simply checks for this attribute and sets the appropriate root.

You can recreate the issue by performing the following steps.
Environment:
macOS 10.12.6
python 3.6 (or 3.4) via anaconda

  1. Create a basic weasyprint program weasyprint_frozen.py:
import weasyprint
print(weasyprint.__version__)
input('Press enter to exit the program: ')
  1. Freeze the program with PyInstaller. Note some files from WeasyPrint need to be bundled with the frozen application (that's what the --add-data options are for):
pip install pyinstaller
pyinstaller weasyprint_frozen.py --name 'Weasyprint App' --log-level ERROR --clean --add-data '/Users/Jonnymetz/anaconda/lib/python3.6/site-packages/pyphen/dictionaries:pyphen/dictionaries' --add-data '/Users/Jonnymetz/anaconda/lib/python3.6/site-packages/weasyprint/css/html5_ph.css:css' --add-data '/Users/Jonnymetz/anaconda/lib/python3.6/site-packages/weasyprint/css/html5_ua.css:css' --onefile

As WeasyPrints currently stands, the frozen application will not run and the traceback should look similar to this:

Traceback (most recent call last):
  File "weasyprint_frozen.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "/Users/Jonnymetz/anaconda/envs/pyweasy36/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages/weasyprint/__init__.py", line 376, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "/Users/Jonnymetz/anaconda/envs/pyweasy36/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages/weasyprint/html.py", line 40, in <module>
  File "site-packages/weasyprint/__init__.py", line 261, in __init__
  File "contextlib.py", line 81, in __enter__
  File "site-packages/weasyprint/__init__.py", line 338, in _select_source
FileNotFoundError: [Errno 2] No such file or directory: '/Users/Jonnymetz/github/basic_flask/dist/css/html5_ua.css'
[24858] Failed to execute script weasyprint_frozen

With my code change, the frozen application should run as expected:

0.41
Press enter to exit the program: 

@liZe
Copy link
Member

liZe commented Nov 16, 2017

Thank you for this pull request.

Well… I'm not really happy each time I add some code that can't be tested and that's specific to some random external tools. The "frozen" hack had been added a long time ago in #269, and I'm really sad to see that there's no way to use the same standard hack with two different tools that are supposed to do the same thing. I don't know what I'll do when someone else will come saying "please add some random code using some random non-standard private variable for my XXX tool that's supposed to work with standard Python programs but that actually needs to change the libraries in a way that will probably change in two years or so when YYY will replace XXX".

OK. That's not your fault. I won't cry this time for two little lines that will help you and may help others.

@liZe liZe merged commit 8aaf786 into Kozea:master Nov 16, 2017
@liZe liZe added this to the v0.42 milestone Nov 16, 2017
@johnnymetz johnnymetz deleted the fix-frozen-root branch November 16, 2017 21:54
@Cheaterman
Copy link

Cheaterman commented Apr 8, 2019

PyInstaller isn't a random tool. I had issues packaging WeasyPrint but I didn't get to follow this in detail (the --add-data args), I will try again now. Thanks to both of you!

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

Successfully merging this pull request may close these issues.

3 participants