Mailing List Archive

Import a lot of modules, or, import 'foo'
So, basically, I'd like to do this:

tests = ['fs', 'swap']

for t in tests:
import t

do more stuff on what promises to be a lot of modules

I've looked around a bit, but can't find what I need. Maybe I'm just
going about this the wrong way, but... Hmm, now I want to know how to
do this in any case:-)

Thanks,
--
David N. Welton, Web Engineer, Linuxcare, Inc.
415.354.4878 x241 tel, 415.701.7457 fax
dwelton@linuxcare.com, http://www.linuxcare.com/
Linuxcare. At the center of Linux.
Import a lot of modules, or, import 'foo' [ In reply to ]
Hi David,

This seems to work for me:

>>> for t in tests:
exec ('import %s' % t)


--

Emile van Sebille
emile@fenx.com
-------------------


David N. Welton <dwelton@linuxcare.com> wrote in message
news:874silabe7.fsf@linuxcare.com...
>
> So, basically, I'd like to do this:
>
> tests = ['fs', 'swap']
>
> for t in tests:
> import t
>
> do more stuff on what promises to be a lot of modules
>
> I've looked around a bit, but can't find what I need. Maybe I'm just
> going about this the wrong way, but... Hmm, now I want to know how to
> do this in any case:-)
>
> Thanks,
> --
> David N. Welton, Web Engineer, Linuxcare, Inc.
> 415.354.4878 x241 tel, 415.701.7457 fax
> dwelton@linuxcare.com, http://www.linuxcare.com/
> Linuxcare. At the center of Linux.
Import a lot of modules, or, import 'foo' [ In reply to ]
"Emile van Sebille" <emile@fenx.com> writes:

> Hi David,
>
> This seems to work for me:
>
> >>> for t in tests:
> exec ('import %s' % t)

Eewwwww, that's kind of twisted, but I guess if it works:-)

Thanks,
--
David N. Welton, Web Engineer, Linuxcare, Inc.
415.354.4878 x241 tel, 415.701.7457 fax
dwelton@linuxcare.com, http://www.linuxcare.com/
Linuxcare. At the center of Linux.
Import a lot of modules, or, import 'foo' [ In reply to ]
On 30 Jul 1999 17:52:32 -0700, dwelton@linuxcare.com (David N. Welton)
wrote:

>
>So, basically, I'd like to do this:
>
> tests = ['fs', 'swap']
>
> for t in tests:
> import t
>
> do more stuff on what promises to be a lot of modules
>
>I've looked around a bit, but can't find what I need. Maybe I'm just
>going about this the wrong way, but... Hmm, now I want to know how to
>do this in any case:-)
>
>Thanks,
>--
>David N. Welton, Web Engineer, Linuxcare, Inc.
>415.354.4878 x241 tel, 415.701.7457 fax
>dwelton@linuxcare.com, http://www.linuxcare.com/
>Linuxcare. At the center of Linux.

you can use the builtin function __import__

See
http://www.pythonware.com/people/fredrik/fyi/fyi06.htm
for more info
Import a lot of modules, or, import 'foo' [ In reply to ]
"David N. Welton" wrote:

> "Emile van Sebille" <emile@fenx.com> writes:
>
> > This seems to work for me:
> >
> > >>> for t in tests:
> > exec ('import %s' % t)
>
> Eewwwww, that's kind of twisted, but I guess if it works:-)
>

I do a similar thing occasionally, and the big problem is that it makes
the Traceback system
work poorly, so that tracking down where unexpected exceptions have
occurred during
debugging can be a pain (especially Tkinter or Pmw generated exceptions,
etc.).

But seriously, you should use the __import__ command instead. ie.:

string = __import__("string")

which is similar (equivalent at the module level?) to:

import string

To import names from within the module (ala. "from spam_module import
eggs"):

globals()[name] = getattr(__import__(module_name), name)

Chad
Import a lot of modules, or, import 'foo' [ In reply to ]
All:
I see some great replies and extra info given on this NG. Is someone
moving this to a FAQ or resource or must one do searches all over the place
for it? If not is deja news the only way to search this NG?

Josef.


Chad Netzer <chad@vision.arc.nasa.gov> wrote in message
news:37A27A54.A737AE62@vision.arc.nasa.gov...
> "David N. Welton" wrote:
>
> > "Emile van Sebille" <emile@fenx.com> writes:
> >
> > > This seems to work for me:
> > >
> > > >>> for t in tests:
> > > exec ('import %s' % t)
> >
> > Eewwwww, that's kind of twisted, but I guess if it works:-)
> >
>
> I do a similar thing occasionally, and the big problem is that it makes
> the Traceback system
> work poorly, so that tracking down where unexpected exceptions have
> occurred during
> debugging can be a pain (especially Tkinter or Pmw generated exceptions,
> etc.).
>
> But seriously, you should use the __import__ command instead. ie.:
>
> string = __import__("string")
>
> which is similar (equivalent at the module level?) to:
>
> import string
>
> To import names from within the module (ala. "from spam_module import
> eggs"):
>
> globals()[name] = getattr(__import__(module_name), name)
>
> Chad
>
>
Import a lot of modules, or, import 'foo' [ In reply to ]
"T. C. Mits" wrote:

> I see some great replies and extra info given on this NG. Is someone
> moving this to a FAQ or resource or must one do searches all over the place
> for it? If not is deja news the only way to search this NG?

As with most newsgroups, dejanews is the second place to look for info.
The FAQ is the first: http://www.python.org/doc/FAQ.html

All PSA members can submit/modify FAQ entries online. I don't know
how often people actually modify the FAQ, but I generally only do it
to add a *frequently* asked question. Otherwise, I just let dejanews
archive the thread. If you'd really like to modify the FAQ yourself,
join the PSA! The membership year starts Oct. 1.

Since the FAQ is searchable, should PSA members make an effort to add
lots of entries to the FAQ? I'm always torn between the "all the info
in one place" and "so much info in one place that people drown"
philosophies.
Does anyone have a good rule of thumb for Python FAQ?

-------------------------------------------
Tom Bryan
Applied Research Laboratories
University of Texas at Austin