hello () hello everyoneĪs you can see using importlib.reload reloads the module and references to the module are updated as well. > from importlib import reload > reload ( foo ) > bar. hello () hello # now we go into foo.py and change print('hello') to print('hello everyone') We can then do the following experiment: > import foo, bar > bar. Let’s suppose we have two modules foo.py and bar.py where bar imports foo and uses a function defined therein: # foo.py The difference between reloading the module this way, which I do not recommend anyone actually does, and using importlib.reload is that this particular way of reloading only reloads the module in the current namespace. This is would per se not be all that bad, however this hacked reload does not facilitate the same behaviour as importlib.reload. hello () hello everyone # change the fileĭef hello (): print ( 'hello' ) # back in the interpreter # module testĭef hello (): print ( 'hello everyone' ) # in the interpreter The code still runs, all functions that were in the module previously are still there, as is the module itself, but something odd happens if you execute import test again: it reimports the module. But what happens if you delete the module from sys.modules first? If you import a module, let’s call it test, modify the file and import again (in the same interpreter instance) nothing changes, you’re still running the old code. This got me thinking “How much does the sys.modules dict actually influence the import process.” and as it turns out a lot and it allows you to mess with it. The interesting thing about that is that unlike mappingproxy which is the dict-like object/wrapper/imitator that a lot of the builtin data structures (such as the dict itself) use to imitate a dict while avoiding modification 1 and infinite recursion 2 this sys.modules standard dict supports item assignment _setitem_ as well as deletion _delitem_. Messing with sys.modulesĭavid Beazley also mentions in his talk that the instance actually recording imported modules is located in sys.modules which happens to be a standard python dict. If you find yourself playing with the idea of reimporting modules in software that’ll be used productively consider alternatives such as writing unittests (if you’re using it to test code while developing), using multi-/subprocess to run the code in a separate interpreter, refactoring or simply restarting the interpreter.Īlso in this article I deliberately try to make programs fail and break which is intended to explore features of the interpreter and standard library and not meant to be done in productive software. It can cause severe bugs which can be virtually impossible to track down. As you’ll see towards the end, things can get very messy very quickly when you reimport modules. I’d like to take this opportunity to disclaim here immediately and sort of spoil the conclusion by stating that you should absolutely avoid reimporting any modules in python. The canonical albeit still bad way is to import importlib and use importlib.reload(module). Reimporting in Python - BasicsĪs David mentions, in python you cannot just import module again to reload the module. So I am sitting here watching David Beazley’s pycon talk about modules, packages and imports and he is talking about sys.modules sort of guarding multiple imports which inspired me to fire up the python interpreter myself and start messing about.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |