[Python-Dev] Dropping __init__.py requirement for subpackages

Phillip J. Eby pje at telecommunity.com
Wed Apr 26 20:11:40 CEST 2006


At 02:07 PM 4/26/2006 -0400, Phillip J. Eby wrote:
> def find_module(self, fullname, path=None):
> # Note: we ignore 'path' argument since it is only used via
>meta_path
> subname = fullname.split(".")[-1]
> if os.path.isdir(os.path.join(self.path, subname)):
> return self
> path = [self.path]
> try:
> file, filename, etc = imp.find_module(subname, path)
> except ImportError:
> return None
> return ImpLoader(fullname, file, filename, etc)

Feh. The above won't properly handle the case where there *is* an __init__ 
module. Trying again:
 def find_module(self, fullname, path=None):
 subname = fullname.split(".")[-1]
 path = [self.path]
 try:
 file, filename, etc = imp.find_module(subname, path)
 except ImportError:
 if os.path.isdir(os.path.join(self.path, subname)):
 return self
 else:
 return None
 return ImpLoader(fullname, file, filename, etc)
There, that should only fall back to __init__-less handling if there's no 
foo.py or foo/__init__.py present.


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /