Using linecache with py2exe
If you are using the linecache module with your Python program, you may find that compiling it with py2exe creates what appears to be a valid build, but the application itself doesn't work because linecache.getline() returns "" instead of the values you were expecting. This apparently happens because py2exe fiddles with the module when it compiles, removing certain functionality. I have no idea why it does this, but seems to.
One possible fix for this is to do the following:
- Find the linecache.py (or linecache.pyc) file on your system. It should be located somewhere like C:\python25\Lib\linecache.pyc
- Copy this file into your main program directory (where the main script for your application is found)
- Rename the file to linecacheCopy.pyc
- Instead of using "import linecache" at the beginning of your script, use "import linecacheCopy"
- Replace all instances of "linecache.getline()", "linecache.checkcache()" etc... with "linecacheCopy.getline()", "linecacheCopy.checkcache()" etc....
- Rerun py2exe
I found this corrected my problem immediately. I'm not sure of the exact reason, but changing the module name seems to stop the compiler messing around with it, restoring functionality. If anyone knows the exact reason linecache doesn't compile correctly, I would be grateful if you could leave a comment below.
