FAQ
List:

Is it possible to determine the name of the module that invoked import from
within the imported module?

Thanks,
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-list/attachments/20060321/7fc1ffb1/attachment.htm

Search Discussions

  • Peter Hansen at Mar 21, 2006 at 2:54 pm

    Eric White wrote:
    Is it possible to determine the name of the module that invoked import
    from within the imported module?
    Almost anything is possible in Python. On the other hand, some things
    are probably impractical, and others ill-advised. Perhaps you can
    describe your requirements, *why* you want to do this, to provide some
    context for the answers.

    Also, consider that the effect of the import statement is different
    after the first import. When an already-imported module is imported by
    another module (i.e. the second time "import xxx" is executed for your
    module), the code is not reloaded from the .pyc file, but instead a
    reference is retrieved from sys.modules where it was cached when the
    first import was executed. What are you expecting to happen in that case?

    -Peter
  • Eric White at Mar 21, 2006 at 5:04 pm
    Peter:

    Thanks for the reply. Consider the following situation:

    A set of variable names is defined along with a list of possible values for
    each. A second set of variable names is defined along with an expression
    for generating a value for each. For each possible permutation of variables
    from the first set, a python script is created that contains commands that
    initialize the variable set with the permuted values. A second python
    script is created that contains commands for connecting to a database and
    storing the values of the second set of variables after running the
    associated expression for each.

    We would like to be able to use the scripts above to drive a python script
    containing a system of equations specified at run time and to store the
    results. This "master" script contains the following calls:

    import somemodule

    # do stuff ...

    # initialize permuted variables
    somemodule.init()

    # do more stuff ...

    # store results
    somemodule.save()

    # do more stuff ... Etc.

    We would like for the call to init to initialize variables in the context of
    the "master" script. To do this inside somemodule we use the
    sys.modulesdictionary to find a reference to the master module by name
    and initialize
    variables at this reference.

    What I would like to know if it is arbitrarily possible to "walk" the import
    hierarchy from any point. It is not apparent that python's optimization of
    subsequent imports of the same module is relavent.
    On 3/21/06, Peter Hansen wrote:

    Eric White wrote:
    Is it possible to determine the name of the module that invoked import
    from within the imported module?
    Almost anything is possible in Python. On the other hand, some things
    are probably impractical, and others ill-advised. Perhaps you can
    describe your requirements, *why* you want to do this, to provide some
    context for the answers.

    Also, consider that the effect of the import statement is different
    after the first import. When an already-imported module is imported by
    another module (i.e. the second time "import xxx" is executed for your
    module), the code is not reloaded from the .pyc file, but instead a
    reference is retrieved from sys.modules where it was cached when the
    first import was executed. What are you expecting to happen in that case?

    -Peter

    --
    http://mail.python.org/mailman/listinfo/python-list
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://mail.python.org/pipermail/python-list/attachments/20060321/76a66f03/attachment.html
  • Steve Holden at Mar 21, 2006 at 6:00 pm

    Eric White wrote:
    Peter:

    Thanks for the reply. Consider the following situation:

    A set of variable names is defined along with a list of possible values
    for each. A second set of variable names is defined along with an
    expression for generating a value for each. For each possible
    permutation of variables from the first set, a python script is created
    that contains commands that initialize the variable set with the
    permuted values. A second python script is created that contains
    commands for connecting to a database and storing the values of the
    second set of variables after running the associated expression for each.

    We would like to be able to use the scripts above to drive a python
    script containing a system of equations specified at run time and to
    store the results. This "master" script contains the following calls:

    import somemodule

    # do stuff ...

    # initialize permuted variables
    somemodule.init()

    # do more stuff ...

    # store results
    somemodule.save()

    # do more stuff ... Etc.

    We would like for the call to init to initialize variables in the
    context of the "master" script. To do this inside somemodule we use the
    sys.modules dictionary to find a reference to the master module by name
    and initialize variables at this reference.

    What I would like to know if it is arbitrarily possible to "walk" the
    import hierarchy from any point. It is not apparent that python's
    optimization of subsequent imports of the same module is relavent.
    It seems to me that you could achieve the same end by accessing the
    calling stack frame inside the calls to your somemodule.functions().
    Whether this is acceptable or not (and whether you are prepared to put
    up with the necessary voodoo) is another question.

    However, this would lead to atrocious coupling in your program. It's the
    kind of thing people used to use Fortran common blocks for, for goodness
    sake!

    The point Peter was trying to make about the distinction between first
    import of a module and subsequent ones is, I suspect, quite relevant, as
    you made it appear that you wanted to rely on code executed *during the
    import* to distinguish its importer. Clearly this would only be possible
    on the first import, as subsequent imports wouldn't execute the module's
    code.

    But now it appears that the import structure isn't really as relevant as
    the calling relationships anyway, no?

    Or am I misunderstanding your intent?

    regards
    Steve

    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd www.holdenweb.com
    Love me, love my blog holdenweb.blogspot.com
  • Eric White at Mar 21, 2006 at 9:12 pm
    I found a builtin function called "execfile" that seems to suite my needs
    quite well. The import function is less applicable. Thanks for your time.
    On 3/21/06, Steve Holden wrote:

    Eric White wrote:
    Peter:

    Thanks for the reply. Consider the following situation:

    A set of variable names is defined along with a list of possible values
    for each. A second set of variable names is defined along with an
    expression for generating a value for each. For each possible
    permutation of variables from the first set, a python script is created
    that contains commands that initialize the variable set with the
    permuted values. A second python script is created that contains
    commands for connecting to a database and storing the values of the
    second set of variables after running the associated expression for each.
    We would like to be able to use the scripts above to drive a python
    script containing a system of equations specified at run time and to
    store the results. This "master" script contains the following calls:

    import somemodule

    # do stuff ...

    # initialize permuted variables
    somemodule.init()

    # do more stuff ...

    # store results
    somemodule.save()

    # do more stuff ... Etc.

    We would like for the call to init to initialize variables in the
    context of the "master" script. To do this inside somemodule we use the
    sys.modules dictionary to find a reference to the master module by name
    and initialize variables at this reference.

    What I would like to know if it is arbitrarily possible to "walk" the
    import hierarchy from any point. It is not apparent that python's
    optimization of subsequent imports of the same module is relavent.
    It seems to me that you could achieve the same end by accessing the
    calling stack frame inside the calls to your somemodule.functions().
    Whether this is acceptable or not (and whether you are prepared to put
    up with the necessary voodoo) is another question.

    However, this would lead to atrocious coupling in your program. It's the
    kind of thing people used to use Fortran common blocks for, for goodness
    sake!

    The point Peter was trying to make about the distinction between first
    import of a module and subsequent ones is, I suspect, quite relevant, as
    you made it appear that you wanted to rely on code executed *during the
    import* to distinguish its importer. Clearly this would only be possible
    on the first import, as subsequent imports wouldn't execute the module's
    code.

    But now it appears that the import structure isn't really as relevant as
    the calling relationships anyway, no?

    Or am I misunderstanding your intent?

    regards
    Steve

    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd www.holdenweb.com
    Love me, love my blog holdenweb.blogspot.com

    --
    http://mail.python.org/mailman/listinfo/python-list
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://mail.python.org/pipermail/python-list/attachments/20060321/97967977/attachment.htm

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 21, '06 at 1:49p
activeMar 21, '06 at 9:12p
posts5
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase