Reputation: 403
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.
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?
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.
Upvotes: 2
Views: 1452
Reputation: 23211
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.
Upvotes: 4
Reputation: 113814
If the expression datetime.date
appears only in app_helper.py
, then the only module that has to import datetime
is app_helper.py
.
Similarly, if the only module that the expression random.random
appears in is app_helper_helper.py
, then the only module that needs to import random
is app_helper_helper.py
.
Higher-level modules can live in blissful ignorance of what the lower-level modules are importing.
Upvotes: 2
Reputation: 251345
If code within module A (that is, code written in A.py
) needs to use module B, module A needs to import module B. That's it.
So if app_helper
makes reference to datetime
, it needs to do import datetime
(or from datetime import datetime
or some such thing, depending on what exactly it needs to use). If app
does not make reference to datetime
, it does not need to do import datetime
.
Upvotes: 1