Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Module imports

Reply
Thread Tools

Module imports

 
 
tkpmep@hotmail.com
Guest
Posts: n/a
 
      01-30-2006
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

 
Reply With Quote
 
 
 
 
Gary Herron
Guest
Posts: n/a
 
      01-30-2006
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
>
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic imports + relative imports in Python 3 zildjohn01 Python 0 02-22-2011 05:24 PM
Imports System.Data or Imports System.Data.SqlClient? Albert ASP .Net 4 07-10-2008 09:00 AM
Mutual module imports Tony Nelson Python 2 10-18-2005 04:49 AM
module imports and memory usage Brad Tilley Python 4 12-01-2004 06:01 PM
Enabling/disabling module imports based on a setting Adrian Holovaty Python 0 11-06-2004 06:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57