Python interactive reload file
The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Python interactive console which automatically reload modules Ask Question. Asked 9 years, 6 months ago. Active 6 years, 8 months ago. Viewed 1k times. Improve this question. Stephan Kulla. Stephan Kulla Stephan Kulla 7 7 bronze badges. Add a comment. Active Oldest Votes. Currently your code involves 4 indentation levels.
Other things: os. Improve this answer. There is a better way. The recommended solution is to use the importlib. This function is designed exactly for reimporting modules that have already been imported before. To reload your module, you need to run:. And if you are not using IPython, this is where your options end. But IPython users have some other interesting solutions to this problem.
It will run all the commands as if you would copy and paste them in your IPython session. You can rerun a file as many times as you want and it will always update all the functions. Running a file in IPython is extremely easy:. It is, but it requires more typing:. To be honest, if I had to type all this, I might as well just use the importlib. All 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.
And typing this importlib. You can with reload re-read a file even without restarting the software with the reload command. Note that any variable pointing to anything in the module will need to get reimported after the reload. Something like this:. One way to do this is to call reload. Calling reload is one way to ensure that your module is up-to-date even if the file on disk has changed. It's not necessarily the most efficient you might be better off checking the last modification time on the file or using something like pyinotify before you reload , but it's certainly quick to implement.
One reason that Python doesn't read from the source module every time is that loading a module is relatively expensive -- what if you had a kb module and you were just using a single constant from the file? Python loads a module once and keeps it in memory, until you reload it. I used the following when importing all objects from within a module to ensure web2py was using my current code:.
The IPython docs cover this feature called the autoreload extension. Originally, I found this solution from Jonathan March's blog posting on this very subject see point 3 from that link. Basically all you have to do is the following, and changes you make are reflected automatically after you save:.
I'm not really sure that is what you mean, so don't hesitate to correct me. You are importing a module - let's call it mymodule. Python will not look for changes in mymodule. It will normally also save the compiled bytecode mymodule. The next time you will start your program, it will check if mymodule. Of course, it is more complicated than that and you may have side-effects depending on what you do with your program regarding the other module, for example if variables depends on classes defined in mymodule.
Alternatively, you could use the execfile function or exec , eval , compile. I had the exact same issue creating a geoprocessing script for ArcGIS I had a python toolbox script, a tool script and then a common script. Dev would run the code in the dev folder, test from test folder and prod from prod folder. Changes to the common dev script would not run when the tool was run from ArcCatalog.
Closing ArcCatalog made no difference. Even though I selected Dev or Test it would always run from the prod folder. The cases will be different for different versions of python. Following shows an example of python 3. For earlier python versions like 2. Better is to use ipython3 as it provides autoreload feature.
0コメント