6

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

10

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()
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.