[Python-Dev] python package

M.-A. Lemburg mal@lemburg.com
Sun, 14 Jul 2002 19:53:32 +0200


Gordon McMillan wrote:
>>>Two of them appear to rely on an implementation
>>>detail - that __path__ (only defined for
>>>imp.PKG_DIRECTORY's) will be followed even in
>>>a plain module.
>>
>>AFAIK, that's not an implementation detail, but a
>>documented way of finding out whether a module is a
>>package or not.
> 
> 
> Correct. But stuffing a __path__ attribute into
> a module does *not* make the module a package.
> 
> '''Whenever a submodule of a package is loaded, Python makes sure that the package itself is loaded first, loading its __init__.py file if necessary.'''
> 
> and
> 
> '''Once loaded, the difference between a package and a module is minimal.'''

Hmm, I know that Python itself uses __path__ to tell
whether it has a package or not, so I don't see why
a module can't be regarded as package. Moving the
module into a directory of the same name and then
renaming it to __init__.py has the same effect. And
in that case, hacking __path__ is perfectly legal.

>>But it works (tm) :-)
> 
> 
> For a sufficiently short-sighted definition of "work".

You haven't commented on the sys.modules trick yet. This
one doesn't even use the __path__ hackery :-)

DateTime.py:
import sys
import mx.DateTime
sys.modules[__name__] = mx.DateTime

Python 2.1.3 (#1, May 16 2002, 18:59:26)
 >>> import DateTime
 >>> DateTime.now()
<DateTime object for '2002-07-14 19:51:03.34' at 82307a0>
 >>> DateTime
<module 'mx.DateTime' from '/home/lemburg/projects/mx/DateTime/__init__.py'>
 >>> id(DateTime)
135726540
 >>> from mx import DateTime
 >>> id(DateTime)
135726540

See: it's the same module :-)

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
_______________________________________________________________________
eGenix.com -- Makers of the Python mx Extensions: mxDateTime,mxODBC,...
Python Consulting:                               http://www.egenix.com/
Python Software:                    http://www.egenix.com/files/python/