Importing v reloading modules modules

Terry Reedy tjreedy at udel.edu
Fri Mar 19 14:02:02 EDT 2010


Reload is effectively deprecated in 2.x and removed in 3.x, so I 
recommend not using it.

Deleting an entry from sys.modules should force recreation with an 
import. Just make sure all other references are also gone so that the 
module object can be deleted.

The following shows how to import by name in string and globally from 
within a function.

def f():
     global itertools
     itertools = __import__('itertools')

f()
print(itertools) # 3.1

#<module 'itertools' (built-in)>

Terry Jan Reedy




More information about the Python-list mailing list