1

This question may be pretty elementary, but here goes:

I'm writing an app that will need lots of classes. I want to basically bundle these classes in to groups. To do that I am basically creating folders and putting the class .py files in those folders.

So an example folder structure might be this:

mainApp.py
moreAppCode.py
functions
  |- mathstuff.py
  |- my_object.py
  `- network.py
gui
  |- wxMainForm.py
  |- wxSplashScreen.py
  `- wxAboutBox.py

Right now, the only way I've found to easily reference these classes in "import" statements is to create __init__.py files in the two folders and inside those init files, import each .py file. So I have to do something like:

(functions/__init__.py)
from mathstuff import mathstuff
from my_object import my_object
from network import network

This does allow me to then do something like import functions in mainApp.py.

However, it seems to me that there should be an easier and simpler way to achieve this.

I don't really want to have to do things like import gui.wxMainForm.wxMainForm (each .py file only contains one class represented by its filename). Also, this precludes doing anything like import gui.* (that isn't valid). So no matter what I find I end up having to manually import every single .py file, either in the init files or in the actual .py file planning to use them.

I can mitigate some of the mess by using from gui import wxMainForm but then I still have to do wxMainForm.wxMainForm(). I could maybe do from gui.wxMainForm import wxMainForm? But then we're still back to a huge mess of import statements at the top of whatever code file I'm working with.

The whole point of all this is I would like to not have to depend on that init file to get this done, because if I ever need to rename a class or add one it means a trip into init to add it or change it there - something easy to forget to do, and difficult to debug!

Have I missed something?

Thanks

-f

fdmillion
  • 4,823
  • 7
  • 45
  • 82
  • Python isn't magic. It just looks, acts, and feels like magic. Also, this is a [duplicate](http://stackoverflow.com/questions/1057431/loading-all-modules-in-a-folder-in-python) – Maggy May May 08 '13 at 04:58
  • 2
    Why are you only putting one class in each file? Just put related classes into the same file. – BrenBarn May 08 '13 at 04:58
  • 1
    I put one class per file mostly for organization. I could put multiple classes in one file but then I lose myself in a huge 100K code file looking for that one function in that one class. I'm a fan of small code files that organize your code well, rather than amalgamations of loads of code in one big file... – fdmillion May 08 '13 at 06:58
  • `__init__.py` files are exactly for that: turning a subdirectory into a Python package. If you want to make things easier like `from gui import *`, or `import gui` and and have everything as `gui.`, use [`__all__`](http://docs.python.org/2/tutorial/modules.html#importing-from-a-package). If you want even more "magic", you could look into dynamically loading modules using the `imp` module, that I would not recommend that: every large project that I know off imports subpackages and modules manually into the larger namespace. –  May 08 '13 at 16:04
  • As for renaming a class or file and having to do that in `__init__.py` as well: that is one line extra, and that's so easily learned that I don't see how you'd forget that ("rename something, alter `__init__.py`"; I can make that a mantra). But, if you use `from import *` in your `__init__.py` and use `__all__` correctly, even that does not need to be done. –  May 08 '13 at 16:07
  • There is typically a lot of middle ground between "one class per file" and "100K lines of code in a file". Do all the files under "functions" actually add up to enough that it becomes hard to navigate? You may have to split things, but it makes sense to split them in terms of what works. Don't make extra work for yourself by splitting one class per file unless you actually need that extremely fine-grained division. If your classes aren't huge, it should be perfectly manageable to put a few related classes in one file and thus simplify your imports. – BrenBarn May 09 '13 at 01:14

0 Answers0