import hook

Thomas Heller theller at python.net
Sun Jun 11 13:01:59 EDT 2006


Alex Martelli wrote:
> 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/> ...

There are also other ways.  You could extend __path__ of foo, and the
pkgutil module might also be useful.

Thomas




More information about the Python-list mailing list