Using the imp module

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Tondering

    #1

    Using the imp module

    I understand that you can use the imp module to programmaticall y mimic
    the "import xyzzy" statement.

    But is there any way to programmaticall y mimic the "from xyzzy import
    *" statment?

    --
    Claus Tondering

  • Fredrik Lundh

    #2
    Re: Using the imp module

    Claus Tondering wrote:
    >I understand that you can use the imp module to programmaticall y mimic
    the "import xyzzy" statement.
    "imp" sounds like overkill for that purpose; the usual way is do do that is to
    explicitly call __import__:

    xyzzy = __import__("xyz zy")
    But is there any way to programmaticall y mimic the "from xyzzy import
    *" statment?
    you can use getattr() to pick out the objects your need. or you could do
    something like:

    globals().updat e(vars(__import __("xyzzy")))

    </F>



    Comment

    • Steve Holden

      #3
      Re: Using the imp module

      Claus Tondering wrote:
      I understand that you can use the imp module to programmaticall y mimic
      the "import xyzzy" statement.
      >
      But is there any way to programmaticall y mimic the "from xyzzy import
      *" statment?
      >
      See the docs for __import__().

      I think imp is pretty much dead nowadays.

      regards
      Steve
      --
      Steve Holden +44 150 684 7255 +1 800 494 3119
      Holden Web LLC/Ltd http://www.holdenweb.com
      Skype: holdenweb http://holdenweb.blogspot.com
      Recent Ramblings http://del.icio.us/steve.holden

      Comment

      • Claus Tondering

        #4
        Re: Using the imp module

        Fredrik Lundh wrote:
        globals().updat e(vars(__import __("xyzzy")))
        Thank you, I can use that in some cases. However, I need to import a
        file that is not in the load path. The imp module allows me to specify
        a path name; but as far as I know, the __import__ function does not
        allow me to do that.

        --
        Claus Tondering

        Comment

        • Claus Tondering

          #5
          Re: Using the imp module

          I wrote:
          Fredrik Lundh wrote:
          globals().updat e(vars(__import __("xyzzy")))
          >
          Thank you, I can use that in some cases. However, I need to import a
          file that is not in the load path. The imp module allows me to specify
          a path name; but as far as I know, the __import__ function does not
          allow me to do that.
          Oops, I forgot to add that I realize that the update(vars(... )) feature
          works well with the imp module too.

          --
          Claus Tondering

          Comment

          • Fredrik Lundh

            #6
            Re: Using the imp module

            Claus Tondering wrote:
            Thank you, I can use that in some cases. However, I need to import a
            file that is not in the load path.
            that's pretty straightforward :

            path = list(sys.path)
            sys.path.insert (0, "directory" )
            try:
            module = __import__("mod ule")
            finally:
            sys.path[:] = path # restore

            or, often as useful,

            namespace = {}
            execfile("direc tory/module.py", namespace)

            the latter executes the external code everything you run it (unlike import,
            which uses the sys.modules cache), and puts everything defined by the
            external script into the given dictionary.

            to import all that into your own module, you can do:

            globals().updat e(namespace)

            for extra bonus, you may want to check the __all__ attribute, and if that's
            not present, filter out anything that starts with an underscore:

            all_names = namespace.get(" __all__")
            if all_names is None:
            all_names = (key for key in namespace where key[0] != "_")
            my_namespace = globals()
            for name in all_names:
            my_namespace[name] = namespace[name]

            </F>



            Comment

            Working...