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

Using WeasyPrint with Pyinstaller #530

Closed
JauntyJJS opened this Issue Nov 6, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@JauntyJJS

JauntyJJS commented Nov 6, 2017

Hi,

I tried to convert a python script that depends on weasyprint into an exe file using pyinstaller.
The good news is that pyinstaller is able to create a folder called "weasyprint" containing the necessary dependencies such as the css files in weasyprint/css and files in weasyprint/tests/resources.

However, the code in html.py assumes that these file dependencies are in the same directory as the exe files.
Around line 36

if hasattr(sys, "frozen"): 
    root = os.path.dirname(sys.executable)
...
HTML5_UA_STYLESHEET = CSS(filename=os.path.join(root, 'css', 'html5_ua.css'))
HTML5_PH_STYLESHEET = CSS(filename=os.path.join(root, 'css', 'html5_ph.css'))

This may not be suitable if someone uses the pyinstaller to create a standalone exe file instead.

I suggest the following modification based on how pyinstaller works.

if hasattr(sys, "frozen"):
    # MEIPASS explanation:
    # https://pythonhosted.org/PyInstaller/#run-time-operation
    root = getattr(sys, '_MEIPASS', None) #To handle standalone exe case 
    if not root:
        root = os.path.dirname(sys.executable) #To handle exe with file dependencies in the same directory
    root = os.path.join(root,'weasyprint')
else:
    root = os.path.dirname(__file__)
HTML5_UA_STYLESHEET = CSS(filename=os.path.join(root, 'css', 'html5_ua.css'))
HTML5_PH_STYLESHEET = CSS(filename=os.path.join(root, 'css', 'html5_ph.css'))

Hope this helps.

PS:
Please note that weasyprint also depends on the file dependencies from the pyphen package as well.
For this package, pyinstaller will not create a folder called "pyphen" by default.
To do so, please write in the *.spec file

import pyphen
pyphen_root = os.path.dirname(pyphen.__file__)
pyphen_dictionaries = Tree(os.path.join(pyphen_root, 'dictionaries'), prefix = 'pyphen/dictionaries')

and put the variable pyphen_dictionary into

exe = EXE(pyz,
          pyphen_dictionaries,
          ... (other necessary inputs)
@liZe

This comment has been minimized.

Show comment
Hide comment
@liZe

liZe Nov 16, 2017

Member

Fixed in #540.

Member

liZe commented Nov 16, 2017

Fixed in #540.

@liZe liZe closed this Nov 16, 2017

@liZe liZe added this to the v0.42 milestone Nov 16, 2017

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