Accessing global namespace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tonguç Yumruk

    Accessing global namespace

    I'm trying to build a completely plug-in based system. One of my
    problems is importing a package dynamically. I'm trying to emulate the
    import command. The __import__() function or imp module doesn't help me
    much because they only return the module. I want to register the module
    with it's name in the current namespace. I can do it by:

    globals()[module_name] = __import__(modu le_name)

    But I don't think it's a good thing to access the global namespace
    directly. I prefer using setattr() but I cannot access the current
    namespace as an object I want something like

    setattr(__main_ _,__import__(mo dule_name))

    Is that possible? I'm using Python 2.1 on Debian Woody

    --
    Love, Respect, Linux
    ############### ############### ############### ############### ############### #
    "Has anyone had problems with the computer accounts?"
    "Yes, I don't have one."
    "Okay, you can send mail to one of the tutors ..."
    -- E. D'Azevedo, Computer Science 372
    ############### ############### ############### ############### ############### #
    Tonguç Yumruk

  • Peter Otten

    #2
    Re: Accessing global namespace

    Tonguç Yumruk wrote:
    [color=blue]
    > I'm trying to build a completely plug-in based system. One of my
    > problems is importing a package dynamically. I'm trying to emulate the
    > import command. The __import__() function or imp module doesn't help me
    > much because they only return the module. I want to register the module
    > with it's name in the current namespace. I can do it by:
    >
    > globals()[module_name] = __import__(modu le_name)
    >
    > But I don't think it's a good thing to access the global namespace
    > directly. I prefer using setattr() but I cannot access the current
    > namespace as an object I want something like
    >
    > setattr(__main_ _,__import__(mo dule_name))
    >
    > Is that possible? I'm using Python 2.1 on Debian Woody
    >[/color]

    Try (tested with 2.2 and 2.3).

    import sys

    setattr(sys.mod ules[__name__], "myos", __import__("os" ))

    sys.modules is a dictionary of all imported modules. It even has a
    "__main__" key.

    Peter

    Comment

    • Peter Hansen

      #3
      Re: Accessing global namespace

      Tonguç Yumruk wrote:[color=blue]
      >
      > I'm trying to build a completely plug-in based system. One of my
      > problems is importing a package dynamically. I'm trying to emulate the
      > import command. The __import__() function or imp module doesn't help me
      > much because they only return the module. I want to register the module
      > with it's name in the current namespace. I can do it by:
      >
      > globals()[module_name] = __import__(modu le_name)
      >
      > But I don't think it's a good thing to access the global namespace
      > directly.[/color]

      There's nothing wrong with "accessing the global namespace directly"
      like that. In fact, that's pretty much the only way (though it's not
      in itself sufficient) to emulate the "import" statement when you want
      a dynamic import.

      In general, you need to (a) import with __imoprt__, (b) insert name
      into sys.modules dictionary, and (c) insert name into globals(). What
      you are doing above is the proper way to do that, IMHO.

      -Peter

      Comment

      • John Roth

        #4
        Re: Accessing global namespace


        "Tonguç Yumruk" <trooper@ttnet. net.tr> wrote in message
        news:mailman.10 65436150.27416. python-list@python.org ...[color=blue]
        > I'm trying to build a completely plug-in based system. One of my
        > problems is importing a package dynamically. I'm trying to emulate the
        > import command. The __import__() function or imp module doesn't help me
        > much because they only return the module. I want to register the module
        > with it's name in the current namespace. I can do it by:
        >
        > globals()[module_name] = __import__(modu le_name)
        >
        > But I don't think it's a good thing to access the global namespace
        > directly. I prefer using setattr() but I cannot access the current
        > namespace as an object I want something like
        >
        > setattr(__main_ _,__import__(mo dule_name))
        >
        > Is that possible? I'm using Python 2.1 on Debian Woody[/color]

        The easiest way to do a dynamic import is to build an
        import statement and feed it into the exec statement.

        On the other hand, I'd question why you actually want
        the name in the global namespace of your module. Might
        putting it in some kind of data structure be better for your
        program logic? I don't know enough about your application
        to tell, though.

        John Roth
        [color=blue]
        >
        > --
        > Love, Respect, Linux
        >[/color]
        ############### ############### ############### ############### ############### #[color=blue]
        > "Has anyone had problems with the computer accounts?"
        > "Yes, I don't have one."
        > "Okay, you can send mail to one of the tutors ..."
        > -- E. D'Azevedo, Computer Science 372
        >[/color]
        ############### ############### ############### ############### ############### #[color=blue]
        > Tonguç Yumruk
        >[/color]


        Comment

        Working...