0

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.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
Fergus
  • 437
  • 3
  • 11

1 Answers1

1

The best compromise seems to rename at import:

import package.module as mod

mod.function(param)

Typing three letters should be short enough. At the same time the import is qualified and does not pollute your global name space as the * import. You are free to use any name for mod. Even a one-letter name would work. But is recommended to have at least three letters in a name.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • This is indeed logical @MikeMüller, however, if I'm importing many functions from across modules and packages, the import statements can grow exponentially. I suppose it would be possible to create an import script to handle all of the function imports and then simply call that from the main application although I've never tried. How are extremely large applications handled where there may be 50+ imports? – Fergus Dec 27 '17 at 22:51
  • You need to import what you need. You still need the same amount import statements, no matter if you use the `*` or my method. – Mike Müller Dec 27 '17 at 23:41
  • Thanks again @MikeMüller. How would I go about creating a file that contains all of my imports and then simply call that from the `__main__.py` application? It seems like that would clean up the import process within the main app. – Fergus Dec 27 '17 at 23:46
  • 1
    Just import what you really need in each module. This should reduce the number of inports considerably. – Mike Müller Dec 28 '17 at 00:19
  • There is also `from package import module`, if the module name is unique enough. (The `as` aliasing syntax is still support with this style of import, of course.) – jpmc26 Dec 28 '17 at 01:00