Problems with import hooks and encoding

Vinay Sajip vinay_sajip at
Wed May 20 08:15:49 EDT 2009

The simple program

 def main():
     print repr(u'\u2029'.encode('utf-8'))

 if __name__ == "__main__":

works as expected when run from the command-line, but fails when
converted to an executable using PyInstaller. (For example, if the
executable is created on a Ubuntu Jaunty system but then run on a
Ubuntu Hardy system.) Digging into why this happens leads to the
observation that Py_Initialize calls _PyUnicode_Init, which in turn
causes encodings to be imported via the _codecs module (codecs.c).
However, this appears to happen before the import hook machinery is
set up. Is there a way of intercepting the importing of encodings so
that a "freeze" processor like PyInstaller's launcher can redirect the
import to the package bundled with the executable, rather than any
package provided with a host-installed version of Python?


Vinay Sajip

More information about the Python-list mailing list