multiversion flag and auto requiring import hook

Andrea Crotti andrea.crotti.0 at
Thu Jan 19 08:12:22 EST 2012

I'm using the multiversion flag in setuptools (-m) to be able to run
many different projects, without modifying the global environment.

Then thanks to pkg_resources magic and setuptools I can get
automatically everything loaded.

Now the problem is that we want to be able to run tests.  The best way
would be to use python test, which would make everything needed
for the tests available.

The problem is that it's very slow to run and harder to integrate in
Eclipse, so I created an import hook, that tries to do a
pkg_resources.require first.

The test runner should do something like

with AutoRequire():
    # run tests

But now I'm fighting with nose and plugins, and I'm not sure this
approach is really correct...

Anyone has a better idea??

Here is the import hook for the auto_import...

--8<---------------cut here---------------start------------->8---
class AutoRequireImport(object):
     """This require import automatically requires with pkg_resources
     all the modules that need to be imported.

     def find_module(self, module_name, package=None):
         #TODO: try to filter to only the important modules
         mod_parent = module_name.split('.')[0]
         except ImportError:
             except pkg_resources.DistributionNotFound:
                 logger.error("not able to require the module %s, you 
need to run a related project or develop the egg with dev_main" % 
                 #TODO: raise the right error to be catched from 
somewhere else?
       "%s required correctly" % mod_parent)

class AutoRequire(object):
     """Context manager to enable and disable the auto-require import hook

     def __init__(self): = AutoRequireImport()

     def __enter__(self):
         logger.debug("enable the auto require import hook")
         return self

     def __exit__(self, type, value, traceback):
         logger.debug("disable the auto require import hook")
--8<---------------cut here---------------end--------------->8---

More information about the Python-list mailing list