[Distutils] Problem with ihooks: not working as expected in Py25 with eggs?

Giovanni Bajo rasky at develer.com
Mon Oct 23 17:11:28 CEST 2006


Hello,

I'm using the following recipe (which I shall publish one day in the
cookbook):

============== nobarepyc.py =========================
import ihooks
import os

class _NoBarePycHooks(ihooks.Hooks):
    def load_compiled(self, name, filename, *args, **kwargs):
        sourcefn = os.path.splitext(filename)[0] + ".py"
        if not os.path.isfile(sourcefn):
            raise ImportError('forbidden import of bare .pyc file: %r' %
filename)
        return ihooks.Hooks.load_compiled(name, filename, *args, **kwargs)

ihooks.ModuleImporter(ihooks.ModuleLoader(_NoBarePycHooks())).install()
============== nobarepyc.py =========================


This is useful when managing Python source trees with CVS/SVN: after a file
is renamed or moved, you don't want imports to silently import the
previously generated .pyc file. By importing this module in debug builds,
you install an import hook that makes so a "bare" .pyc file (with no .py
file next to it) is never imported.

Now, this file does not seem to work with packages shipped as .egg files, at
least in Python 2.5. Specifically, I get an import error with setuptools
itself:

>>> from misc import nobarepyc
>>> import pkg_resources
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python25\lib\ihooks.py", line 404, in import_module
    q, tail = self.find_head_package(parent, str(name))
  File "C:\Python25\lib\ihooks.py", line 447, in find_head_package
    raise ImportError, "No module named " + qname
ImportError: No module named pkg_resources

*IF* I can read this traceback correctly, this has nothing to do with my own
hook: it's ihooks itself which appears to be broken with .egg files. Is that
correct? Does .egg files break ihooks? What is the correct way of writing
this sort of import hooks with setuptools / .egg files?

Thanks
-- 
Giovanni Bajo



More information about the Distutils-SIG mailing list