blackmetalmcdonalds blackmetalmcdonalds - 3 months ago 17
Python Question

Pyinstaller and Ply IOError: source code not available

I am pretty new to pyinstaller but I have been banging my head against this problem for a couple of days now and I can't seem to figure out what's wrong. My script runs fine normally but throws IOerror when i try to build with pyinstaller, my modules (including ply.lex) seem to be included but maybe i'm being an idiot? If anyone has any advice it would be much appreciated...

Here's my error (line 65 is where my lexer is built)

Traceback (most recent call last):
File "<string>", line 65, in <module>
File "site-packages/ply/lex.py", line 906, in lex
File "site-packages/ply/lex.py", line 580, in validate_all
File "site-packages/ply/lex.py", line 822, in validate_rules
File "site-packages/ply/lex.py", line 833, in validate_module
File "inspect.py", line 690, in getsourcelines
File "inspect.py", line 529, in findsource
IOError: source code not available


If anyone has seen this problem before, or can help that would be awesome.

Answer

PLY insists that its grammars be defined in files. Real files, with names and everything. I think that's because of its strategy to cache the computed grammar tables, which involves comparing the timestamp of the cached tables with the timestamp of the original file.

Pyinstaller is, apparently, evaluating the grammar as a <string>, since it is extracted from an archive rather than being a file. (The Pyinstaller manual mentions that __file__ is not correctly set for the frozen application, and that is what PLY is looking at.) You might try using the --onedir option when you create your installer bundle, but of course that has slightly different behaviour.