__import__() with packages

Discussion in 'Python' started by Marco Herrn, Apr 4, 2004.

  1. Marco Herrn

    Marco Herrn Guest

    Hi,

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

    m= __import__("eggs")

    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__("eggs", 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__("spam.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
    (GnuPG/PGP-signed and crypted mail preferred)
    Key ID: 0x94620736
     
    Marco Herrn, Apr 4, 2004
    #1
    1. Advertising

  2. Marco Herrn

    Paul Moore Guest

    Marco Herrn <> writes:

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


    [...]

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


    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
     
    Paul Moore, Apr 4, 2004
    #2
    1. Advertising

  3. Marco Herrn

    John Roth Guest

    "Marco Herrn" <> wrote in message
    news:c4p3f2$2j1tnb$-berlin.de...
    > Hi,
    >
    > I am using the builtin __import__() to import modules. That works for
    > simple modules like in this example:
    >
    > m= __import__("eggs")
    >
    > 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__("eggs", 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__("spam.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?


    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__("spam.eggs")
    eggs = spam.eggs

    This will probably also work:

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

    HTH

    John Roth
    >
    > Marco
    >
    > --
    > Marco Herrn
    > (GnuPG/PGP-signed and crypted mail preferred)
    > Key ID: 0x94620736
    >
     
    John Roth, Apr 4, 2004
    #3
  4. Marco Herrn

    Marco Herrn Guest

    On 2004-04-04, John Roth <> wrote:
    > m = __import__("spam.eggs")
    > eggs = spam.eggs
    >
    > This will probably also work:
    >
    > eggs = __import__("spam.eggs").eggs


    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
    (GnuPG/PGP-signed and crypted mail preferred)
    Key ID: 0x94620736
     
    Marco Herrn, Apr 4, 2004
    #4
  5. Marco Herrn

    Marco Herrn Guest

    Sorry, accidently sent out the unfinished message.

    On 2004-04-04, John Roth <> wrote:
    > m = __import__("spam.eggs")
    > eggs = spam.eggs
    >
    > This will probably also work:
    >
    > eggs = __import__("spam.eggs").eggs


    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
    (GnuPG/PGP-signed and crypted mail preferred)
    Key ID: 0x94620736
     
    Marco Herrn, Apr 4, 2004
    #5
  6. Marco Herrn

    Hung Jung Lu Guest

    Marco Herrn <> wrote in message news:<c4pdmu$2lnh1i$-berlin.de>...
    > 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.


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

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

    regards,

    Hung Jung
     
    Hung Jung Lu, Apr 6, 2004
    #6
    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. Paul  Smith
    Replies:
    0
    Views:
    734
    Paul Smith
    Nov 18, 2003
  2. Coder Coder

    __import__

    Coder Coder, May 8, 2004, in forum: Python
    Replies:
    4
    Views:
    706
    Heiko Wundram
    May 8, 2004
  3. David Lyon
    Replies:
    19
    Views:
    584
    David Lyon
    Apr 24, 2009
  4. Phil
    Replies:
    2
    Views:
    465
  5. Ian
    Replies:
    3
    Views:
    172
Loading...

Share This Page