I'm on a Windows 7 machine with QGIS 2.8 installed via OSGeo4W. I'm developing several processing scripts that make use of a few common pieces of code. Rather than maintain the code in each individual script file, I'd like to import the common functions into each script.
These scripts may be shared between multiple users so I'd like to avoid having code stored in multiple locations. My initial thought was to save _foo.py within the .qgis2\processing\scripts folder on my user account and then import via
import _foo
However, Processing doesn't set the scripts directory as the CWD of the python interpreter so the utility script doesn't get picked up.
Is there a way to achieve my goal without turning this into a full-blown QGIS plugin?
2 Answers 2
The script can be placed in any paths that the environment variable PYTHONPATH has.
If some of the default locations don't suit your need, then you can make changes to PYTHONPATH as needed.
In QGIS, you can append or prepend the PYTHONPATH variable.
Check out the System
tab in the Settings > Options
window.
Place a check beside Use custom variables (restart required - include separators)
Then click Add
.
Under Apply
you can change that to prepend
or append
.
Then set the Variable
as PYTHONPATH
and the Value
to the path of your python scripts.
-
Thanks. That appears to be the most sensible solution. I was hoping to avoid introducing a lot of additional steps when deploying for other users so being able to just copy a couple of files into a single directory would have been ideal.spencerrecneps– spencerrecneps2015年07月20日 18:52:27 +00:00Commented Jul 20, 2015 at 18:52
If you want to put the python script in a folder which already exists you can open the python console in qgis and type
import sys
sys.path
You can choose any of the directories that are listed.
-
Unless I'm mistaken, this wouldn't allow me to run the scripts within the Processing framework. Isn't that correct?spencerrecneps– spencerrecneps2015年07月20日 19:10:03 +00:00Commented Jul 20, 2015 at 19:10
-
@spencerrecneps No it won't, but it does show you the list of paths that are part of the PYTHONPATH.SaultDon– SaultDon2015年07月20日 19:25:04 +00:00Commented Jul 20, 2015 at 19:25