It used to be a built-in function in Python 2, but this is no longer the case. In Python, you can reload a module that has already been imported using the reload() function from the importlib module. For that, it is required that I reimport the. Python 3 supports inline reloading of modules using a function called, well clever enough, reload. The aim of this page is to share a tactical line I use when writing a python module and testing it in REPL. The Python reload module is useful in situations like an interactive session where we are frequently running any test script because in such cases we always use the module's first version even if any changes have been made in the code. The problem is, when I do changes to the Python code (like, add a new function), they are not immediately usable, because neither the Python REPL, nor IPython / Jupyter would auto-reload them. The Python reload module is used in reloading any module that has previously been imported into the program. With time, I have developed this practice of opening up a Python REPL (though I recommend IPython or the Jupyter Console for that), importing my in-progress module and singling out separate functions I would like to test. Well, that’s certainly a way, but as modules get bigger, they tend to import other modules, or do some preliminary setup work. Piece of cake! Add the changes and execute the script again. This is dangerous at best, and can frequently be done differently using either exec (ah the evil territory we find ourselves in), or a segregated process.Often, I would work on a Python module and add changes to it live, which I would then want to test immediately. Outside it, sometimes people used reload() to implement plugins etc. Examples of how to reload a python module: Table of contents. The context of using it in the interactive interpreter doesn't leave a lot of choices as to what you are doing, and what the real best solution would be. If you develop your own python module it can be useful to reload it each time you make some change. It doesn't even work on all kinds of modules- extension modules will not reload properly, or sometimes even break horribly, when reloaded. The argument passed to the reload() must be a module object which is successfully imported before. It doesn't work effectively- you might reload a module but leave leftovers from previous versions. The reload() method is used to reload a module. There's really no good use of reload (in fact, it's removed in 3.x). If that's not it, hopefully it's something better, not worse. The only really great solution, though, is using the unittest module. Alternatively, write it out as a program and use python -i. Save one of your sessions to a file and use doctest, for a quick fix. If you then do some work on the submodule. There are lots of ways to test a module out, and doing it by hand in the interactive interpreter is among the worst ways. The scenario that the library addresses is that your code imports some module which, in turn, imports a submodule. My suspicion (backed up by previous experience with people asking similar questions) is that you're testing your module. If you really need to reload modules so often, I've got to ask: why? Then it would just be > from project.models import user It would be better and cleaner if you used the user module directly, rather than doing import * (which is almost never the right way to do it). > import project # get module reference for reload As asked, the best you can do is > from import *
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |