Wednesday, 11 October 2017

python - Re-import custom functions without restarting QGIS



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.



Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...