views:

71

answers:

1

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.

+3  A: 

Well, this is a quite common pattern and I think it is also the reason why you can include things inside __init__.py files.
As a confirmation, just grep for import statements in __init__.py files, and you will see that it is widely used in both standard library and common packages.

Roberto Liffredo
You're right. I just looked at wxPython's `__init__.py` file and they do the same thing.
rmw1985