Created
June 18, 2015 22:44
-
-
Save pjbull/a8493d489d8a31396c09 to your computer and use it in GitHub Desktop.
Reload Package in Active Development (IPython Notebook)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import types | |
import dev_package | |
def reload_package(root_module): | |
package_name = root_module.__name__ | |
# get a reference to each loaded module | |
loaded_package_modules = dict([ | |
(key, value) for key, value in sys.modules.items() | |
if key.startswith(package_name) and isinstance(value, types.ModuleType)]) | |
# delete references to these loaded modules from sys.modules | |
for key in loaded_package_modules: | |
del sys.modules[key] | |
# load each of the modules again; | |
# make old modules share state with new modules | |
for key in loaded_package_modules: | |
print 'loading %s' % key | |
newmodule = __import__(key) | |
oldmodule = loaded_package_modules[key] | |
oldmodule.__dict__.clear() | |
oldmodule.__dict__.update(newmodule.__dict__) | |
reload_package(dev_package) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment