Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > __import__() with packages

Reply
Thread Tools

__import__() with packages

 
 
Marco Herrn
Guest
Posts: n/a
 
      04-04-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 http://www.velocityreviews.com/forums/(E-Mail Removed)
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736

 
Reply With Quote
 
 
 
 
Paul Moore
Guest
Posts: n/a
 
      04-04-2004
Marco Herrn <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
John Roth
Guest
Posts: n/a
 
      04-04-2004

"Marco Herrn" <(E-Mail Removed)> wrote in message
news:c4p3f2$2j1tnb$(E-Mail Removed)-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 (E-Mail Removed)
> (GnuPG/PGP-signed and crypted mail preferred)
> Key ID: 0x94620736
>



 
Reply With Quote
 
Marco Herrn
Guest
Posts: n/a
 
      04-04-2004
On 2004-04-04, John Roth <(E-Mail Removed)> 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 (E-Mail Removed)
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736

 
Reply With Quote
 
Marco Herrn
Guest
Posts: n/a
 
      04-04-2004
Sorry, accidently sent out the unfinished message.

On 2004-04-04, John Roth <(E-Mail Removed)> 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 (E-Mail Removed)
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736

 
Reply With Quote
 
Hung Jung Lu
Guest
Posts: n/a
 
      04-06-2004
Marco Herrn <(E-Mail Removed)> wrote in message news:<c4pdmu$2lnh1i$(E-Mail Removed)-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Packages : A loomingproblem? packages might no longer work? (well not on your platform or python version anyway) David Lyon Python 19 04-23-2009 11:10 PM
Advice on converting hashed packages to pseudo-hashed packages Ian Perl Misc 3 02-12-2005 12:17 AM
Do the javax.imageio packages replace packages in com.sun.image? Paul Smith Java 0 11-18-2003 02:58 PM
VHDL Packages Sandeep VHDL 1 08-14-2003 08:00 AM



Advertisments