Why can't I "from module import *" except at module level?

Discussion in 'Python' started by Mudcat, Jan 13, 2006.

  1. Mudcat

    Mudcat Guest

    I have a directory structure that contains different modules that run
    depending on what the user selects. They are identical in name and
    structure, but what varies is the content of the functions. They will
    only need to be run once per execution.

    Example (directory level):
    Sys1:
    A
    B
    C
    Sys2:
    A
    B
    C
    Sys3:
    A
    B
    C
    Sys4:
    A
    B
    C

    So if the user selects Sys1 during execution, I want to import the
    modules using "from *" and the run the content of those files.

    Now the way my program is set up, it is very important that I be able
    to "from Sys1 import *" and not "import Sys1". The names of the
    functions inside are passed in from somewhere else and the program
    requires those function names to be globally scoped.

    So I have a function that looks like this:

    def importModules( type ):
    cwd = os.getcwd()
    path = cwd + "\\" + type
    sys.path.append(path)

    from security import *

    Obviously this is not working and I get a syntax error at runtime. So
    without this functionality, how do I target modules to import in other
    directories after program execution has begun?

    Thanks
    Mudcat, Jan 13, 2006
    #1
    1. Advertising

  2. Mudcat

    Mudcat Guest

    Anyone?

    Is there any way to do this or am must I load all modules by function
    name only if it's after initialization?
    Mudcat, Jan 13, 2006
    #2
    1. Advertising

  3. Mudcat wrote:

    > Is there any way to do this or am must I load all modules by function
    > name only if it's after initialization?


    Not sure. globals().update(mod.__dict__) might do the trick. Or just design a
    better system and be done with it.
    --
    Giovanni Bajo
    Giovanni Bajo, Jan 14, 2006
    #3
  4. On Fri, 13 Jan 2006 10:17:32 -0800, Mudcat wrote:

    > I have a directory structure that contains different modules that run
    > depending on what the user selects. They are identical in name and
    > structure, but what varies is the content of the functions. They will
    > only need to be run once per execution.

    ....
    > So if the user selects Sys1 during execution, I want to import the
    > modules using "from *" and the run the content of those files.
    >
    > Now the way my program is set up, it is very important that I be able
    > to "from Sys1 import *" and not "import Sys1".


    Then change the way your program is set up :)

    Seriously, "from module import *" is generally a bad idea. Not always, but
    generally, and in this specific instance I'm not sure that your use case
    is one of those exceptions. However, be that as it may be, on to solving
    your problem:

    When you "import foo" or "from module import foo", the name foo is bound
    in the local scope, not global. In other words, the name foo will then
    only exist inside the function. So this does not work:

    >>> def local_import():

    .... import string
    ....
    >>> local_import()
    >>> string.whitespace

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name 'string' is not defined


    However this does work as you would expect:

    >>> def func_import():

    .... global math
    .... import math
    ....
    >>> func_import()
    >>> math.sin

    <built-in function sin>


    See http://docs.python.org/ref/import.html for more detail on what happens
    when you import. Keep in mind that it states that "from module import *"
    in function blocks is explicitly stated to be an error, and in Python 2.3
    a warning is raised:


    >>> def wild_import():

    .... from math import *
    ....
    <stdin>:1: SyntaxWarning: import * only allowed at module level




    > The names of the
    > functions inside are passed in from somewhere else and the program
    > requires those function names to be globally scoped.
    >
    > So I have a function that looks like this:
    >
    > def importModules( type ):
    > cwd = os.getcwd()
    > path = cwd + "\\" + type
    > sys.path.append(path)
    >
    > from security import *
    >
    > Obviously this is not working and I get a syntax error at runtime.


    Should security be defined somewhere?

    > So
    > without this functionality, how do I target modules to import in other
    > directories after program execution has begun?


    I'd consider putting your import logic into a module of its own, at
    the module-level and not inside a function. Then just call "from importer
    import *" in the top level of your code and I think that should meet your
    needs.



    --
    Steven.
    Steven D'Aprano, Jan 14, 2006
    #4
    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. John Salerno
    Replies:
    20
    Views:
    819
    John Salerno
    Aug 11, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,733
    Smokey Grindel
    Dec 2, 2006
  3. pabbu
    Replies:
    8
    Views:
    705
    Marc Boyer
    Nov 7, 2005
  4. Fabio Z Tessitore

    who is simpler? try/except/else or try/except

    Fabio Z Tessitore, Aug 12, 2007, in forum: Python
    Replies:
    5
    Views:
    361
  5. David House

    try -> except -> else -> except?

    David House, Jul 6, 2009, in forum: Python
    Replies:
    2
    Views:
    322
    Bruno Desthuilliers
    Jul 6, 2009
Loading...

Share This Page