Exposing classes inside modules within a Python package directly in the package's namespace

Posted by Richard Waite on Stack Overflow See other posts from Stack Overflow or by Richard Waite
Published on 2010-04-11T19:39:49Z Indexed on 2010/04/11 19:43 UTC
Read the original article Hit count: 276

Filed under:
|
|

I have a wxPython application with the various GUI classes in their own modules in a package called gui. With this setup, importing the main window would be done as follows:

from gui.mainwindow import MainWindow

This looked messy to me so I changed the __init__.py file for the gui package to import the class directly into the package namespace:

from mainwindow import MainWindow

This allows me to import the main window like this:

from gui import MainWindow

This looks better to me aesthetically and I think it also more closely represents what I'm doing (importing the MainWindow class from the gui "namespace"). The reason I made the gui package was to keep all the GUI stuff together. I could have just as easily made a single gui module and stuffed all the GUI classes in it, but I think that would have been unmanageable. The package now appears to work like a module, but allows me to separate the classes into their own modules (along with helper functions, etc.).

This whole thing strikes me as somewhat petty, I just thought I'd throw it out there to see what others think about the idea.

© Stack Overflow or respective owner

Related posts about python

Related posts about wxpython