9/7/2023 0 Comments Python reload module![]() ![]() It used to be a built-in function in Python 2, but this is no longer the case. Python 3 supports inline reloading of modules using a function called, well clever enough, reload. 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 argument must be a module object, so it must have been successfully imported before. ![]() 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. when you use the from foo import bar, unless bar is a module (it looks like it isn't, in your case) you will have to use another import statement. The function takes in two numbers and returns their sum. Fortunately, since I'm testing the code independently, I don't have to do this a ton, but it's still a major hassle.Often, I would work on a Python module and add changes to it live, which I would then want to test immediately. 1 Answer Sorted by: 3 You must have a module to reload. Here, we have defined a function add() inside a module named example. It does not refresh the imported modules, so I have to close and re-open ArcMap whenever I change something there. Without exiting the REPL, change mod.py: s 'Computers are useless. You can use inspect.getmodule to get the module that owns a function: import inspect from math import sqrt from importlib import reload math reload (inspect.getmodule (sqrt)) sqrt math.sqrt. ![]() If you then do some work on the submodule. Within a module, the module’s name (as a string) is available as the value of the global variable name. The file name is the module name with the suffix. This works out really well but I've run into one frustrating issue during development. If you make a change to a module and need to reload it, you need to either restart the interpreter or use a function called reload () from module importlib. The scenario that the library addresses is that your code imports some module which, in turn, imports a submodule. A module is a file containing Python definitions and statements. autoreload tries to work around common pitfalls by replacing function code objects and parts of classes previously in the module with new versions. Supporting_module.do_something(some_input, some_output) Reloading Python modules in a reliable way is in general difficult, and unexpected things may occur. Supporting code is kept in separate unit-testable modules. ![]() pyt file itself is simply a wrapper to collect input arguments and define the tools themselves. When using a python toolbox (.pyt) in ArcMap, I'll typically follow a pattern where the. 1 1 asked at 13:34 Jonathan Livni 101k 103 264 359 Add a comment 8 Answers Sorted by: 83 As of today, the proper way of doing this is: import sys, importlib importlib.reload (sys.modules 'foo') from foo import bar Tested on python 2.7, 3.5, 3.6. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |