Is there a way to import all modules within a package from the root level main application and have functions within the modules immediately available in Python 3? Here is an example file tree:
+app
|- __init__.py
|- main.py
|+ package
|- __init__.py
|- module.py #(contains function1, function2 and function 3)
I would like to avoid the following import statement within the main.py (it can get long and sloppy for complex applications):
from package.module import function1, function2, function3
This is nice, because it then lets me call function1()
from main.py.
Importing a function from the package.module alone means I have to then call the full directory of the function to use it within my script (or alias it within a new function). For example:
import package.module
package.module.function(param)#not fun...too long!
#or like this
def newFunction(param):
'''why create a new function to handle an already existing one?
Bad form and non Pythonic'''
return package.module.function(param)
If I include __all__ = ["module1", "module2"]
in the __init__.py
of my package, that means I have to do the following:
from package import * #it is said this is bad form as well
module.function(param)
#I just want the function and don't want to call module also
My goal is to import the package and module and then have full use of all of the functions within module without needing to explicitly call out the package and the module each time I want to use the function. Thinking something along the lines of:
import package.module
function1(x)
I have read the documentation for Python 3 about modules and packages. Thoughts/help are appreciated.