3

I'm trying to create an use PyInstaller in my project.

This project uses weasyprint but when I run pyinstaller, it fails because weasyprint's resource files seems not being installed.

Traceback (most recent call last):
  File "<string>", line 20, in <module>
  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
  File "/usr/local/lib/python3.4/dist-packages/PyInstaller-3.0dev_be553f0-py3.4.egg/PyInstaller/loader/pyi_importers.py", line 302, in load_module
    exec(bytecode, module.__dict__)
  File "/usr/local/lib/python3.4/dist-packages/mdedit-1.0.1-py3.4.egg/mdedit/editor/mdeditor.py", line 13, in <module>
    from mdedit.generator import *
  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
  File "/usr/local/lib/python3.4/dist-packages/PyInstaller-3.0dev_be553f0-py3.4.egg/PyInstaller/loader/pyi_importers.py", line 302, in load_module
    exec(bytecode, module.__dict__)
  File "/usr/local/lib/python3.4/dist-packages/mdedit-1.0.1-py3.4.egg/mdedit/generator.py", line 13, in <module>
    from weasyprint import HTML, CSS
  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
  File "/usr/local/lib/python3.4/dist-packages/PyInstaller-3.0dev_be553f0-py3.4.egg/PyInstaller/loader/pyi_importers.py", line 302, in load_module
    exec(bytecode, module.__dict__)
  File "/usr/local/lib/python3.4/dist-packages/weasyprint/__init__.py", line 337, in <module>
    from .html import find_base_url, HTML5_UA_STYLESHEET, get_html_metadata
  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
  File "/usr/local/lib/python3.4/dist-packages/PyInstaller-3.0dev_be553f0-py3.4.egg/PyInstaller/loader/pyi_importers.py", line 302, in load_module
    exec(bytecode, module.__dict__)
  File "/usr/local/lib/python3.4/dist-packages/weasyprint/html.py", line 37, in <module>
    filename=os.path.join(os.path.dirname(__file__), 'css', 'html5_ua.css'))
  File "/usr/local/lib/python3.4/dist-packages/weasyprint/__init__.py", line 223, in __init__
    with result as (source_type, source, base_url, protocol_encoding):
  File "/usr/lib/python3.4/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.4/dist-packages/weasyprint/__init__.py", line 294, in _select_source
    with open(filename, 'rb') as file_obj:
FileNotFoundError: [Errno 2] No such file or directory: '/home/sylvain/git/md-edit/dist/MdEditor/weasyprint/css/html5_ua.css'

Is there a way to force pyinstaller including this file ?

Shall I include it by myself ?

Command : pyinstaller mainwindow.spec

mainwindow.spec :

# -*- mode: python -*-
a = Analysis(['mdedit/mainwindow.py'],
             pathex=['/home/sylvain/git/md-edit'],
             hiddenimports=[],
             hookspath=None,
             runtime_hooks=None)
pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='mainwindow',
          debug=False,
          strip=None,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=None,
               upx=True,
               name='MdEditor')

Thanks in advance.

Garcia Sylvain
  • 356
  • 4
  • 10

2 Answers2

3

I've had a similar problem, possibly the same one, and solved it as follows. Hope it works for you:

# -*- mode: python -*-

from PyInstaller.utils.hooks import collect_data_files

w = collect_data_files('weasyprint')
ww = []
for k,v in w:
    ww.append((k, v.split('weasyprint/')[1]))
added_files += ww
added_files += collect_data_files('pyphen')
extra_imports = ['pyphen', 'weasyprint']

a = Analysis(['mdedit/mainwindow.py'],
             pathex=['/home/sylvain/git/md-edit'],
             datas=added_files,
             hiddenimports=extra_imports,
             hiddenimports=[],
             hookspath=None,
             runtime_hooks=None)
pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='mainwindow',
          debug=False,
          strip=None,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=None,
               upx=True,
               name='MdEditor')
Schwolop
  • 41
  • 2
0

I was facing a similar problem for weasyprint and was able to use @Schwolop 's answer with some modifications, since I was getting errors for 'tinycss2'. Here's the first part of my spec file:

w = collect_data_files('weasyprint')
tinycss = collect_data_files('tinycss2')

ww = []
counter = 0
for k,v in w:
    if counter == 0:
        ww.append((k, '.'))
        counter += 1
    else:
        ww.append((k, v.split('weasyprint/')[1]))

tcss = []
counter = 0
for k,v in tinycss:
    if counter == 0:
        tcss.append((k, '.'))
        counter += 1
    else:
        tcss.append((k, v.split('tinycss2/')[1]))

added_files = ww
added_files += collect_data_files('tinycss2')
added_files += collect_data_files('cairocffi')
added_files += collect_data_files('pyphen')
added_files.extend(more_data)
extra_imports = ['tinycss2', 'cairocffi', 'pyphen', 'weasyprint']
Muhammad Farhan
  • 359
  • 2
  • 4
  • 12