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.

underdark
84.9k22 gold badges237 silver badges418 bronze badges
asked Jul 8, 2016 at 11:14

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()
thipa
1,0873 silver badges13 bronze badges
answered Jul 25, 2016 at 12:31
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.