By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,839 Members | 1,635 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,839 IT Pros & Developers. It's quick & easy.

__import__() with packages

P: n/a
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 he***@gmx.net
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736

Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Marco Herrn <he***@gmx.net> 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
Jul 18 '05 #2

P: n/a

"Marco Herrn" <he***@gmx.net> wrote in message
news:c4*************@ID-165840.news.uni-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 he***@gmx.net
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736

Jul 18 '05 #3

P: n/a
On 2004-04-04, John Roth <ne********@jhrothjr.com> 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 he***@gmx.net
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736

Jul 18 '05 #4

P: n/a
Sorry, accidently sent out the unfinished message.

On 2004-04-04, John Roth <ne********@jhrothjr.com> 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 he***@gmx.net
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736

Jul 18 '05 #5

P: n/a
Marco Herrn <he***@gmx.net> wrote in message news:<c4*************@ID-165840.news.uni-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
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.