Proposal: Inline Import

adam adam_goucher at hotmail.com
Mon Dec 12 21:38:35 CET 2005


When I'm feeling too lazy to do imports while moving large blocks of
code, I use this little hack. It lets me proceed with checking whether
the move does what I wanted and at the end I fix the imports and remove
the try/except wrapper. I think it would achieve your desired result
and not have an impact on the language itself.

try:
    #your main code here
    print string.upper("blah")
except NameError, error_value:
    mod_name = error_value.args[0][error_value.args[0].find("'") +
1:error_value.args[0].rfind("'")]
    try:
        __import__(mod_name)
        print "imported %s" % mod_name
    except:
        print "NameError: %s" % error_value.args[0]
    pass

-adam




More information about the Python-list mailing list