[Python-Dev] python package

M.-A. Lemburg mal@lemburg.com
Thu, 11 Jul 2002 09:43:28 +0200


Guido van Rossum wrote:
>>How about adding
>>
>>python.py:
>>__path__ = ['.']
>>
>>This would not only reserve the name in the global namespace,
>>but also enable applications to start using 'from python import x'
>>now without much fuzz.
> 
> 
> Then I have to ask the question I originally wanted to ask: what
> problem would that solve?  And is this the right solution?

It solves the namespace issue.

Every time we add a module or package to the standard lib, there
is a chance that we break someones code out there by overriding
his/her own module/package (e.g. take the addition of the email
package -- such generic names tend to be used a lot).

Whether it's the right solution depends on how you see it.
IMHO it would be ideal to move the complete std lib under
a single package. You might want to use a more diverse hierarchy
but I don't think that is really needed for the existing
code base. Using a single package also makes the transition
from non-package imports to python-package imports a lot
easier.

> Also, it would make *all* standard modules accessible through the
> python package -- surely this isn't what we want (not if we use the
> Java example at least).

Are you sure that you want to make things complicated ? (see above)

> Also, for some modules (that keep some global state) it's a bad idea
> if they are imported twice, since their initialization code would be
> run twice, and there would be two separate instances of the module.

That's true for the trick I proposed above since the modules
are reachable in two ways with the standard way of writing
'import <stdmod>' being used in tons of code.

Now there is also a different way to approach this problem,
though: that of directing Python to the right package by
providing stubs for all current standard lib modules.

I have used such a stub for my mx stuff when I moved
everything from top-level to under the 'mx' umbrella:

# Redirect all imports to the corresponding mx package
def _redirect(mx_subpackage):
     global __path__
     import os,mx
     __path__ = [os.path.join(mx.__path__[0],mx_subpackage)]
_redirect('DateTime')

# Now load all important symbols
from mx.DateTime import *

This works great -- it even let's you load pickles which
store the old import names and automagically converts
them to the new names.

-- 
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/