find out whether a module exists (without importing it)

Chris Angelico rosuav at gmail.com
Tue Aug 7 05:08:08 EDT 2012


On Tue, Aug 7, 2012 at 6:00 PM, Gelonida N <gelonida at gmail.com> wrote:
> modulename = 'my.module'
> cmd = 'import %s as amodule'
> try:
>     exec(cmd)
>     print "imported successfully"

Someone will doubtless correct me if I'm wrong, but I think you can
avoid exec here with:

amodule=__import__(modulename)

ChrisA



More information about the Python-list mailing list