2

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

4

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.

1
  • 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. Commented Jul 20, 2015 at 18:52
0

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.

2
  • Unless I'm mistaken, this wouldn't allow me to run the scripts within the Processing framework. Isn't that correct? Commented 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
    Commented Jul 20, 2015 at 19:25

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.