Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do a python module's imports need to be carried over when importing that module?

Tags:

python

import

It's late and I'm confused about imports within imports (within imports). For this question, you'll have to bear with my "apps" and "helpers" as they are merely vehicles for an explanation of my python project's structure.

Case 1: imports within imports

Suppose a python script app.py imports a module from app_helper.py. The only point of app_helper.py is to help app.py. It never runs on its own.

app.py

from app_helper import datechecker
import ...

date_val = 20140101-1010
print datechecker(date_val)

app_helper.py

import ...

def datechecker(date_val):
    if (datetime.date.today().year - int(date_val[:4])) < 0:
        return "Error: Invalid year (YYYY) in %s. Too high." % date_val
    else:
        return True

Does import datetime need to be in app.py, app_helper.py, or both?

Case 2: imports within imports within imports...

Furthermore, suppose I've got app_helper_helper.py, which app_helper imports. Like app_helper, app_helper_helper is never executed on its own, its just called by app_helper, which is only ever called by app.py. In this case, there's something in app_helper.py that I didn't show.

app_helper.py

from app_helper_helper import decision
import ...

def datechecker(date_val):
    if (datetime.date.today().year - int(date_val[:4])) < 0:
        return "Error: Invalid year (YYYY) in %s. Too high." % date_val
    else:
        return decision()

app_helper_helper.py

import ...

def decision():
    x = random.random()
    if x > .5:
        return True
    return False

Where do the imports go then?

The answer to Case 1 should determine where import random goes for these two modules, but does app.py need to import random as well? And how about import decision? Does app.py need to do any of that? Or does app_helper.py take care of it? On top of it all, if app.py also needs to make use of app_helper_helper's decision function, then do both app.py and app_helper.py require from app_helper_helper import decision or is it just app.py?

I know the http://docs.python.org/2/tutorial/modules.html should cover most of the bases here, but I guess I just need someone to put it in my terms, or in app-app_helper-app_helper_helper terms.

like image 589
lcary Avatar asked Feb 22 '26 23:02

lcary


1 Answers

Consider the following two scripts:


module1.py

import random
import module2

module2.py

print random.random()   # NameError: name 'random' is not defined

module2 does not know about random, and may very well define its own object named random. As much as that is discouraged, it is not forbidden. In the case of random, a built in module, it is expected that one wouldn't shadow it. But there are thousands of other packages one could download, all using their own module names. You can't be expected to avoid all of them.

So to use the functionality of a module, be it built in like random, a downloaded package, or your own, the calling module must import it. Otherwise it cannot know about it.

There is very little overhead to importing a module in multiple places. The code is not loaded twice. It is only referenced twice.


In short, import what you need in the modules in which you need it.

like image 158
mhlester Avatar answered Feb 24 '26 18:02

mhlester



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!