refactoring a group of import statements

GrayShark howe.steven at gmail.com
Sun Jun 27 18:06:20 EDT 2010


I have a large list of package files to import. I'm using a try/except 
test to verify the import. Looks like:

try:
	import abc
except ImportError:
	print( "Error importing abc" )

I've got many of those segments. I want to try and refactor this part 
of the code. 

Trying:
	for mod in ['ab', 'cd', 'ef' ]:
		try:
			mod = __import__( mod )
		except ImportError:
			print( "Error importing %" % mod )

This of course doesn't work. The module ab get's assign in the application
as mod. 

Tried:
	for mod in ['ab', 'cd', 'ef' ]:
		('%s' % mod ) = __import__( mod )

Still no joy.

I need a way to deference the the string in mod to be just a variable.

Any suggestions?

GrayShark




More information about the Python-list mailing list