__import__() with packages

Marco Herrn herrn at gmx.net
Sun Apr 4 09:44:02 EDT 2004


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             herrn at gmx.net
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736




More information about the Python-list mailing list