Module imports

T

tkpmep

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. 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
 
G

Gary Herron

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top