I am trying to understand what the best practices are with regards to Python's (v2.7) import mechanics. I have a project that has started to grow a bit and lets say my code is organized as follows:
foo/
__init__.py
Foo.py
module1.py
module2.py
module3.py
The package name is foo
and underneath it I have module Foo.py
which contains code for the class Foo
. Hence I am using the same name for the package, module and class which might not be very clever to start with.
__init__.py
is empty and class Foo
needs to import module1, module2 and module3
hence part of my Foo.py
file looks like:
# foo/Foo.py
import module1
import module2
import module3
class Foo(object):
def __init__(self):
....
....
if __name__ == '__main__':
foo_obj = Foo()
However I later revisited this and I thought it would be better to have all imports in the __init__.py
file. Hence my __init__.py
now looks like:
# foo/__init__.py
import Foo
import module1
import module2
import module3
....
....
and my Foo.py
only needs to import foo
:
# foo/Foo.py
import foo
While this looks convenient since it is a one liner, I am a bit worried that it might be creating circular imports. What I mean is that when the script Foo.py
is run it will import everything it can and then __init__.py
will be called which will import Foo.py
again (is that correct?). Additionally using the same name for package, module and class makes things more confusing.
Does it make sense the way I have done it? Or am I asking for trouble?