问题
I have a file utils.py
containing a function called f1()
.
From another Python script I can import utils
or execfile('utils.py')
and have access to f1()
. What are the differences between the two methods?
回答1:
There are many differences, but from your point of view the most significant is probably that import
gives you more control over the namespace in which the objects defined in utils.py
end up.
Let's consider three variants on import
. The first is the one you asked about:
import utils
utils.f1()
utils
is the only symbol that has been added to your workspace—any pre-existing f1
in your base workspace will not have been overwritten, and if there is none, then f1()
on its own will not be recognized. For code I intend to maintain, I greatly prefer this way of importing, because it makes it easy for me to search my source file for all the places in which it depends on utils
.
But if saying utils.f1()
every time is too verbose then you can do this:
from utils import f1
f1()
Now if you say f1()
that will call utils.f1()
, because that is the code object that you have now associated with the name f1
in your workspace. It's now slightly harder to get an overview of where your code is reliant on the utils
module. But at least this type of import
statement gives you precise control over which symbols were imported and which not. You can even rename symbols during this process:
from utils import f1 as EffOne
EffOne()
Finally you can choose to lose control over the namespace entirely:
from utils import *
Now, who knows what symbols have been imported: basically everything that utils
has to offer the world (or, if the utils
developer took the trouble to specify an __all__
attribute, then everything listed there). I'd advise you to use import *
only for quick-and-dirty programming, if at all.
This is actually the importing style that is closest to execfile
from the namespace point of view: execfile('utils.py')
does much the same as from utils import *
in that it dumps all symbols defined by utils
willy-nilly into your workspace. One slight difference is that execfile
won't even limit itself to the symbols in __all__
if that is defined—in fact, the __all__
symbol itself will just get dumped in your lap along with everything else.
Beyond namespaces, there are still plenty of differences between from utils import *
and execfile('utils.py')
. One is caching: a second import
call on utils
will be very fast (the code will not be re-run), but a second call to execfile('utils.py')
may take just as long as the first because the code will be re-run. Also, there may be some code (often test code) inside utils.py
that the utils
author does not want to run at import time, but only when the file is executed via execfile
. Such code is placed inside an if __name__ == '__main__':
clause.
来源:https://stackoverflow.com/questions/27517003/difference-between-import-and-execfile