[Python-Dev] importlib hooray! (original) (raw)

Thomas Heller theller at ctypes.org
Wed Feb 6 10:26:38 CET 2013


I have become a fan of the new python 3.3 importlib in the last few days.

It has allowed me to write a ModuleMapper which I put into sys.metapath (in sitecustomize.py, for Python 3.3).

This mapper currently does rename modules like 'Queue' or '_winreg' to the Python3 modules 'queue' or 'winreg' without the need to change my 2.7 sourcecode (changing the sourcecode is required when using six.moves to write 2.x/3.x compatible code).

The six.moves approach has another problem with freeze tools (py2exe for example): ModuleFinder does not find the moved modules because it cannot track import().

I have also started a new modulefinder which uses importlib to find modules; this was quite easy since I could copy a lot of code from importlib._bootstrap. The great thing is that this new modulefinder is much better than the old one: It finds modules in zipped eggs or other zip-archives which are on sys.path; also it finds the renamed modules correctly that my ModuleMapper has mapped.

The only thing that I am missing is that it is a bit of guesswork to find out the type of the module that importlib.find_loader() has found.

Previously imp.find_module returned a tuple containing the type of module found (like imp.PY_SOURCE), I have to poke around in some attributes of the result of importlib.find_loader() to guess the type.

Thomas



More information about the Python-Dev mailing list