1

I have three files: module_one.py module_two.py module_three.py which each has a class for itself in it:

# module_one.py
from module_two import ModuleTwo
from module_three import ModuleThree
class ModuleOne:
...

# module_two.py
from module_one import ModuleOne
from module_three import ModuleThree
class ModuleTwo:
...

# module_three.py
from module_one import ModuleOne
from module_two import ModuleTwo
class ModuleThree:
...

I have a file main.py which looks like this:

# main.py
from module_one import ModuleOne
from module_two import ModuleTwo
from module_three import ModuleThree
... # and uses them

Which is the entry point of the application and uses those module classes.
In each of those module files (module_*.py) as you see I have to import other modules to use them. Isn't there any better way to manage this? can I write a statement in main.py and after that those module files that get loaded in the memory can see each other and call on themselves without having to import each other in their own files first?

  • There's a pretty good explanation here: https://stackoverflow.com/questions/15890014/python-namespaces-with-module-imports – SuperShoot Jul 13 '17 at 10:38
  • Just as an example, Pygame defines `init()` methods in its main module and submodules You could do something like that. – anonymoose Jul 13 '17 at 11:06
  • 1
    I asked about modules some time ago as well: [Here](https://stackoverflow.com/questions/41871239/how-to-use-init-py-in-sub-modules-to-define-namespaces). // Since I am used to program in C++ I still stick to one to few classes in each file. I only import classes from other modules with `from ... import ...` if they are absolutely necessary in that specific module. – P. Siehr Jul 13 '17 at 11:07
  • Is there any good reason not to put all three classes in a single module, if each one actually requires the other two? Python isn't Java, which only allows a single public class per file. – chepner Jul 13 '17 at 11:42
  • 1
    @chepner IMO this a question of taste. Either you are ok with large files / classes / methods, or you tend to split them. For me size is an indicator to rethink my design. – P. Siehr Jul 13 '17 at 12:01

0 Answers0