Module imports

Gary Herron gherron at islandtraining.com
Mon Jan 30 16:55:47 EST 2006


tkpmep at hotmail.com wrote:

>I have written a number of functions and stored them in a file named
>myFunctions.py. This keeps all my functions under one roof, and if I
>want to use or one or more of them in a program, I can start the
>program off with
>
>from myFunctions import f1,f2
>
>Some of these functions require various math routines, so they in turn
>have an import line
>
>def f1(x,y):
>    from math import log
>    k = log(x)
>
>I'd like to import all the math routines just once with a single import
>at the top of myFunctions.py
>and then rewrite all my functions to use this single import i.e.
>
>import math
>
>def f1(x,y):
>    k = math.log(x)
>
>However, if I now import one of my functions into a program, i.e.
>from myFunctions import f1,f2
>
>f1 and f2 no longer have access the math functions they need. 
>
That's not true.  Have you actually tried this?  It works perfectly, and 
is, in fact, the expected and standard operation procedure.    The 
functions from myFunctions execute in the environment of the module they 
were define in, no matter how you import/reference them from another 
procedure.

Just try it and you'll be please with the results.

Gary Herron

>Is there
>a way to make python automatically execute all the imports it finds at
>the top of myFunctions.py so that all these system functions are
>visible to the functions in myFunctions.py without having to import
>them piecemeal?
>
>Thomas Philips
>
>  
>




More information about the Python-list mailing list