Module imports

Discussion in 'Python' started by tkpmep@hotmail.com, Jan 30, 2006.

  1. Guest

    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
    , Jan 30, 2006
    #1
    1. Advertising

  2. Gary Herron Guest

    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
    >
    >
    >
    Gary Herron, Jan 30, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adrian Holovaty
    Replies:
    0
    Views:
    273
    Adrian Holovaty
    Nov 6, 2004
  2. Brad Tilley

    module imports and memory usage

    Brad Tilley, Nov 30, 2004, in forum: Python
    Replies:
    4
    Views:
    323
    Carlos Ribeiro
    Dec 1, 2004
  3. Albert
    Replies:
    4
    Views:
    10,871
    Albert
    Jul 10, 2008
  4. zildjohn01
    Replies:
    0
    Views:
    649
    zildjohn01
    Feb 22, 2011
  5. Victor Hooi
    Replies:
    1
    Views:
    106
    Devin Jeanpierre
    Nov 25, 2013
Loading...

Share This Page