import hook

Alex Martelli aleax at mac.com
Sun Jun 11 17:22:08 CEST 2006


Jeremy Sanders <jeremy+complangpython at jeremysanders.net> wrote:

> Hi - Is it possible to override the import process so that if in my program
> I do
> 
> import foo.bar
> 
> Python will look for bar in a directory which isn't called foo?
> 
> I want my module/program to be able to be run without being installed in
> site-packages, so by doing "import foo.bar", it should start looking for
> bar in the current directory which could be called "foo-0.43".
> 
> I've tried overriding __import__, chopping out "foo." from package names,
> but that tends to break. I've also tried overriding imp.find_module() but
> Python never appears to use my version.
> 
> Any ideas?

Yes, PEP 302 (which despite being marked as "draft" has in fact been
already mostly implemented, since it's used by the zipimport mechanism)
allows you to perform such feats.  Study it at
<http://www.python.org/dev/peps/pep-0302/> ...


Alex



More information about the Python-list mailing list