I have got myself very confused. I have a set of python functions, all of which I've bunged together in a file called (let's say) useful.py
. I can then read the module into my ipython with
import useful as uf
and then I can access individual functions with
uf.meaning_of_life()
and so on. Standard stuff.
However - some functions in this file call on other functions. I can call a single function any time with uf.
, but what about functions which call each other? If a function called eat
makes reference to another function called chew
, how does eat
know where to find chew
? I can call both as uf.eat
and uf.chew
.
I can ignore all of this by simply doing execfile('useful.py')
which works perfectly well, but I would like to get more of a handle on the module system.
Currently when I use import
my attempt to use my functions produces errors; when I use execfile
everything works fine.
I appreciate that this might be construed as very much a beginners question, but I am coming to Python from a Matlab background, and my natural inclination is to use execfile
. Pointers to information would be very welcome.
eat
andchew
being functions in the same module can refer to each other by their unqualified nameseat
andchew
. – Commiseratefrom useful import *
will import everything except names prefixed with '_'. Then they can be accessed from the current module aschew
rather thanuf.chew
. This practice is generally frowned on, because you suddenly end up not knowing what names are already defined. – Zinsuf.chew
? – Zins