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. Outside it, sometimes people used reload() to implement plugins etc. 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. It doesn't even work on all kinds of modules- extension modules will not reload properly, or sometimes even break horribly, when reloaded. It doesn't work effectively- you might reload a module but leave leftovers from previous versions. 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. There are lots of ways to test a module out, and doing it by hand in the interactive interpreter is among the worst ways. 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 * exc_traceback ) ) ) else : """Īt this point, the code both compiled and ran without error. exc_info ( ) ) + "\r\n" + listf ( format_exception ( sys. """ try : execfile ( modulepath ) except : return Error ( "Error in execution: " + str ( sys. """ try : compile ( code, modulename, "exec" ) except : return Error ( "Error in compilation: " + str ( sys. If compile() fails, the module will not be replaced. read ( ) except : return Error ( "Error opening file: " + modulepath + ". """ try : code = open ( modulepath, 'rU' ). Try to open the specified module as a file Use the imported module to determine its actual path """ try : tmp = _import_ ( modulename ) except : return Error ( "Couldn't import module " + modulename ) """ message def listf ( data ) : buffer = "" for line in data : buffer = buffer + line + "\n" return buffer def recompile ( modulename ) : """įirst, see if the module can be imported at all. success def _str_ ( self ) : return self. success = success def _call_ ( self ) : return self. """ def _init_ ( self, message, success = False ) : self. It can be returned out of a function for display elsewhere and retain When python code is edited, the user should have the option of having the IDE automatically reload the module containing the changes. Code: import sys import string from traceback import print_exc, format_exception class Error : """Īn Error is a string message with a fail/succeed boolean value In order to minimize potential problems, we implement a few checks prior to the actual reloading of the module to ensure that we are at least dealing with valid Python code. It was originally written for a project in which relatively untrustworthy code might be reloaded during operation. This is a short example of how to (more or less) safely reload a python module. Dynamically reload a python module Description:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |