preserving namespaces with sub-packages
Kevin Altis
altis at semi-retired.com
Sat Dec 1 15:02:14 EST 2001
I have a module file in a package that has gotten quite large and I would
like to split it up. Since I don't want to include the whole file, I've
created something that should be equivelant which I'll call 'widgie.py' and
is shown below. I'll call the package 'framework'
---
file: widgie.py
class BaseWidgie:
def __init__( self ):
self.name = 'BaseWidgie instance'
class Button(BaseWidgie):
def __init__( self ) :
self.name = 'Button instance'
---
If I refer to class Button in the current framework, the reference is:
framework.widgie.Button
So, what I would like to do is create a directory in the current package
named 'widgie' that acts as a sub-package and the classes such as Button
above will be referenced the same way. The directory structure looks like:
framework directory
__init__.py
various module files for the framework
widgie directory
__init__.py
basewidgie.py
button.py
And the split files look like:
---
file: basewidgie.py
class BaseWidgie:
def __init__( self ):
self.name = 'BaseWidgie instance'
---
---
file: button.py
from basewidgie import BaseWidgie
class Button(BaseWidgie):
def __init__( self ) :
self.name = 'Button instance'
---
So the question is what should the imports in __init__.py look like so that
the sub-package effectively looks exactly the same as when there was a
single widgie.py module file in the framework package? I must not be
manipulating the namespaces right because so far I end up with stuff like:
framework.widgie.button.Button
In addition, it would be nice have __init__.py automatically load all the
files in the widgie directory rather than having to hard-code each filename.
Hopefully I'm explaining the problem correctly. Any suggestions or
solutions?
ka
More information about the Python-list
mailing list