[Tutor] using dynamic import statements

Alan Gauld alan.gauld at btinternet.com
Mon Jul 9 09:47:04 CEST 2012


On 09/07/12 02:10, Chris Hare wrote:
>
> Here is what I want to do:
>
> I have a bunch of modules to import. instead of duplicating
 > a lot of code for each import, I want to do something like this:

...lots of code stripped....


Why not the more usual:

import sys, os, imp, stat,    \
        re, webbrowser, Image, \
        StringIO, shutil, datetime

And let the Python ImportError alert you to what failed?

Or if you must print your own errors just use

try:
    import ...
except ImportError:
    # your code here

I'm not sure what code you think you need to duplicate or why.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/





More information about the Tutor mailing list