问题
Alright so I have a python project that I want to compile, so I decided to use pyinstaller (first time compiling python). Now it compiled fine but when I run the exe it returns -1. So after a bit of messing around I figured out that it was related to reportlab.platypus.
So my first instinct was to check to see if using hooks changed anything, so I tried adding the reportlab.pdfbase._fontdata
and reportlab.lib.utils
hooks (these were the only hook files I could find related to reportlab). Despite this effort it still failed.
Here is the output when the exe is run from the terminal:
Traceback (most recent call last):
File "<string>", line 12, in <module>
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
File "C:\Users\Jon\Desktop\PyInstaller-3.1.1\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "Board_builder.py", line 5, in <module>
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
File "C:\Users\Jon\Desktop\PyInstaller-3.1.1\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\reportlab\platypus\__init__.py", line 7, in <module>
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
File "C:\Users\Jon\Desktop\PyInstaller-3.1.1\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\reportlab\platypus\flowables.py", line 32, in <module>
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
File "C:\Users\Jon\Desktop\PyInstaller-3.1.1\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\reportlab\lib\styles.py", line 28, in <module>
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
File "C:\Users\Jon\Desktop\PyInstaller-3.1.1\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\reportlab\rl_config.py", line 131, in <module>
File "site-packages\reportlab\rl_config.py", line 102, in _startUp
File "site-packages\reportlab\lib\utils.py", line 695, in rl_isdir
AttributeError: 'FrozenImporter' object has no attribute '_files'
main returned -1
From this I gather that it crashes on running line 5 in "Board_builder.py" (the file that handles reportlab in my project) here are the first 5 lines of that file:
import subprocess
import datetime
from reportlab.lib.units import mm, inch
from reportlab.lib.pagesizes import legal, landscape
from reportlab.platypus import SimpleDocTemplate, Table
I have no idea what the AttributeError it is throwing means, any advice would be very welcome!
回答1:
Well I got it working,
Decided to go look at where exactly the AttributeError was being thrown from so I inspected the reportlab/rl_config.py
and reportlab/lib/utils.py
files and found that it was checking objects recursively looking for directories (as insinuated by rl_isdir
). Some how the FrozenImporter got stuck being checked with a list of other objects
so I replaced the line:
return len(list(filter(lambda x,pn=pn: x.startswith(pn),list(__loader__._files.keys()))))>0
with:
try:
return len(list(filter(lambda x,pn=pn: x.startswith(pn),list(__loader__._files.keys()))))>0
except AttributeError:
return False
This may not have been the cleanest most efficient way to resolve the issue but it only touches one line of the original code so I found this to be the most straight forward solution.
来源:https://stackoverflow.com/questions/35691320/issues-with-pyinstaller-and-reportlab