__import__() with packages

M

Marco Herrn

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
 
P

Paul Moore

Marco Herrn said:
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
 
J

John Roth

Marco Herrn said:
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
 
M

Marco Herrn

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
 
M

Marco Herrn

Sorry, accidently sent out the unfinished message.

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
 
H

Hung Jung Lu

Marco Herrn said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top