__import__() with packages

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marco Herrn

    __import__() with packages

    Hi,

    I am using the builtin __import__() to import modules. That works for
    simple modules like in this example:

    m= __import__("egg s")

    when there is the module "eggs.py" in the current directory

    But how do I do this with packages? A I understand the documentation for
    __import__(), it must be something like:

    m= __import__("egg s", globals(), locals(), ["spam"])

    when there is the package "spam" in the current directory, containing
    the module "eggs".
    But that doesn't work. I tried it in some different forms. The only one
    that works in some way is:

    m= __import__("spa m.eggs")

    But that is not what I want, since I get "spam" as a module:
    <module 'spam' from 'spam/__init__.pyc'>

    So what am I doing wrong here?

    Marco

    --
    Marco Herrn herrn@gmx.net
    (GnuPG/PGP-signed and crypted mail preferred)
    Key ID: 0x94620736

  • Paul Moore

    #2
    Re: __import__() with packages

    Marco Herrn <herrn@gmx.ne t> writes:
    [color=blue]
    > I am using the builtin __import__() to import modules. That works for
    > simple modules like in this example:[/color]

    [...]
    [color=blue]
    > But how do I do this with packages? A I understand the documentation
    > for __import__(), it must be something like:[/color]

    Look again at the documentation for __import__. In particular, you
    want a function like the following, given in the dicumentation:

    def my_import(name) :
    mod = __import__(name )
    components = name.split('.')
    for comp in components[1:]:
    mod = getattr(mod, comp)
    return mod

    Then, you do

    eggs = my_import("spam .eggs")

    I have to admit, I find this annoyingly subtle - 99.99% of the time,
    it's my_import() that you want, but you have to define it yourself...

    Ah, well. I hope this helps.

    Paul
    --
    This signature intentionally left blank

    Comment

    • John Roth

      #3
      Re: __import__() with packages


      "Marco Herrn" <herrn@gmx.ne t> wrote in message
      news:c4p3f2$2j1 tnb$1@ID-165840.news.uni-berlin.de...[color=blue]
      > Hi,
      >
      > I am using the builtin __import__() to import modules. That works for
      > simple modules like in this example:
      >
      > m= __import__("egg s")
      >
      > when there is the module "eggs.py" in the current directory
      >
      > But how do I do this with packages? A I understand the documentation for
      > __import__(), it must be something like:
      >
      > m= __import__("egg s", globals(), locals(), ["spam"])
      >
      > when there is the package "spam" in the current directory, containing
      > the module "eggs".
      > But that doesn't work. I tried it in some different forms. The only one
      > that works in some way is:
      >
      > m= __import__("spa m.eggs")
      >
      > But that is not what I want, since I get "spam" as a module:
      > <module 'spam' from 'spam/__init__.pyc'>
      >
      > So what am I doing wrong here?[/color]

      You're doing everything correctly, just not quite enough.
      What you've got is an almost empty module named "spam",
      which contains another module bound to the identifier "eggs".

      So what you need to do is:

      m = __import__("spa m.eggs")
      eggs = spam.eggs

      This will probably also work:

      eggs = __import__("spa m.eggs").eggs

      HTH

      John Roth[color=blue]
      >
      > Marco
      >
      > --
      > Marco Herrn herrn@gmx.net
      > (GnuPG/PGP-signed and crypted mail preferred)
      > Key ID: 0x94620736
      >[/color]


      Comment

      • Marco Herrn

        #4
        Re: __import__() with packages

        On 2004-04-04, John Roth <newsgroups@jhr othjr.com> wrote:[color=blue]
        > m = __import__("spa m.eggs")
        > eggs = spam.eggs
        >
        > This will probably also work:
        >
        > eggs = __import__("spa m.eggs").eggs[/color]

        Thanks,
        but in my application I read the names of the modules from a file,
        so I do not know them when writing (in this case I wouldn't know the
        name 'eggs'). Since
        --
        Marco Herrn herrn@gmx.net
        (GnuPG/PGP-signed and crypted mail preferred)
        Key ID: 0x94620736

        Comment

        • Marco Herrn

          #5
          Re: __import__() with packages

          Sorry, accidently sent out the unfinished message.

          On 2004-04-04, John Roth <newsgroups@jhr othjr.com> wrote:[color=blue]
          > m = __import__("spa m.eggs")
          > eggs = spam.eggs
          >
          > This will probably also work:
          >
          > eggs = __import__("spa m.eggs").eggs[/color]

          Thanks,
          but in my application I read the names of the modules from a file,
          so I do not know them when writing (in this case I wouldn't know the
          name 'eggs'). Since the solution from Paul works for me, I will use
          that.

          Marco


          --
          Marco Herrn herrn@gmx.net
          (GnuPG/PGP-signed and crypted mail preferred)
          Key ID: 0x94620736

          Comment

          • Hung Jung Lu

            #6
            Re: __import__() with packages

            Marco Herrn <herrn@gmx.ne t> wrote in message news:<c4pdmu$2l nh1i$2@ID-165840.news.uni-berlin.de>...[color=blue]
            > Thanks,
            > but in my application I read the names of the modules from a file,
            > so I do not know them when writing (in this case I wouldn't know the
            > name 'eggs'). Since the solution from Paul works for me, I will use
            > that.[/color]

            There is an easier way: all imported modules are listed in the
            sys.modules namespace dictionary. So,

            import sys
            __import__('spa m.eggs')
            my_module = sys.modules['spam.eggs']

            regards,

            Hung Jung

            Comment

            Working...