I have a python file (in [user]\.qgis2\python
) with some functions that I'm working on. It's just a .py file with a bunch of def function_1
, def function_2
etc. in it. On startup I import the .py file with all the functions from my_functions.py import *
and then play around with them. at this stage I'm still getting lots of errors, so I go back to my python file in a text editor, make some changes and save the file.
How do I get these changes to be reflected within QGIS? I've tried re-importing the .py file but I get the same errors that I've just corrected, so the only way I've been able to use the modified functions is by restarting QGIS and then importing again.
1 Answer 1
When I wanted to re-import the startup.py file after I made some edits, I used:
import imp, startup
# Use last 3 lines to reload script after saving edits
imp.reload(startup)
from startup import *
run_function()
This saved me the trouble of having to restart QGIS.
Since imp has been removed in python version 3.12, use importlib instead:
import importlib, startup
# Use last 3 lines to reload script after saving edits
importlib.reload(startup)
from startup import *
run_function()
-
thanks a lot! It also worked for me in making an update for custom plugin.Pavel Pereverzev– Pavel Pereverzev2022年11月11日 16:47:45 +00:00Commented Nov 11, 2022 at 16:47
-
1imp is now importlib! this gis.stackexchange.com/questions/225444/… is the same question, the only additional information there is the link to stackoverflow.com/questions/1517038/python-refresh-reload, I think.MoritzMoreira– MoritzMoreira2023年06月19日 12:42:49 +00:00Commented Jun 19, 2023 at 12:42