![]() ![]() It will check the modification timestamps of the modules and compare them with the time when they are imported. IPython is quite smart about what to reload. IPython with %autoreload enabled will be slightly slower.Great, any caveats? I found 3 minor ones: Great way to make writing and testing your modules much easier. %autoreload 2 - auto-reload all the modules.It's a good option if you want to specifically auto-reload only a selected module. %autoreload 1 - it will only auto-reload modules that were imported using the %aimport function (e.g %aimport my_module).%autoreload 0 - disables the auto-reloading.There are 3 configuration options that you can set: Now, you can turn on auto-reloading: %autoreload 2Īnd each time you execute some code, IPython will reimport all the modules to make sure that you are using the latest possible versions. It's not enabled by default, so you have to load it as an extension: %load_ext autoreload Wouldn't it be great if there was a way to automatically reload a module? Well, IPython can actually do that! %autoreload to the rescue #Īnother one of the magic methods in IPython is related to reloading modules. And typing this importlib.reload / %run / exec. To be honest, if I had to type all this, I might as well just use the importlib.reload instead.Īll those options are great, but if you are as bad as me when it comes to writing code and you make a lot of mistakes, then it means a lot of reloading. ![]() It is, but it requires more typing: exec ( open ( "./my_file.py" ). I cheated a bit when I said that this option is not available in standard Python REPL. Running a file in IPython is extremely easy: %run my_file. You can rerun a file as many times as you want and it will always update all the functions. It will run all the commands as if you would copy and paste them in your IPython session. If you don't care about actually "importing" your module and all you need is to run some functions defined in a file, you can execute that file instead. But IPython users have some other interesting solutions to this problem. And if you are not using IPython, this is where your options end. So that's how you can reimport a module in Python. To reload your module, you need to run: import importlib This function is designed exactly for reimporting modules that have already been imported before. The recommended solution is to use the importlib.reload function. If your module is referenced from another module, there is a chance that you still won't be able to reimport it. And it will work in most cases, but there are some caveats. Since we know that the interpreter will first look for the module in the sys.modules dictionary, we can just delete our module from this dictionary. But usually you don't want to restart the REPL, so there are better ways. It works fine if you don't care about preserving the data that you already have in your session, like the functions that you wrote and the variables that you calculated. The easiest way is to quit your interactive session and start it again. So, if I can't reimport a module, does it mean that I have to restart Python each time? Not really, that would be very inconvenient. You can read more about how importing works in the documentation. Which means that, if you already imported the module (or imported a different module that references this one) and you try to import it again, Python will ignore this request. And only if it's not there, it actually imports the module.First, it checks if the module is already cached in the sys.module dictionary.Turns out that, for efficiency reasons, when you import a module in an interactive Python session, Python interpreter does two steps: It doesn't matter if you use the standard Python REPL or IPython. If you already imported a module ( import a_module) or a function ( from a_module import a_function) in your Python session and you try to import it again, nothing will happen. It turns out, as StackOverflow kindly explained, that you can't just reimport a module. That's fine - I thought - I will just fix the module and reimport it.īut, to my surprise, calling from my_module import my_function didn't update the code! my_function still had the bug that I just fixed! I double-checked if I modified the correct file, reimported it again and still nothing. As it usually happens, when I was testing it in the interactive Python REPL, the first version turned out to have some bugs (the second and third ones also did □). Writing my first module in Python was a confusing experience. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |