2

I know how to overwrite the TEMP variable path from the QGIS Interface, but I'm having problems doing the same thing via stand-alone Python scripts (using QgsApplication).

For example, I have the following script:

# Prepare QGIS environment
import sys
from qgis.core import *
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("E:\\QGIS\\apps\\qgis", True)
QgsApplication.initQgis()

# Prepare processing framework 
sys.path.append('E:\\QGIS\\apps\\qgis\\python\\plugins')
from processing.core.Processing import Processing
Processing.initialize()
import processing
from processing.tools import *

# Set variables
algorithm = "saga:rastercalculator"
input = "E:/input.tif"
addInput = None
formula = "ifelse(a=0, 0/0, ifelse(a<0, 0/0, a))"
useNoData = False
dataType = 7
result = "E:/output.tif"

# Run algorithm
processing.runalg(algorithm, input, addInput, formula, useNoData,
                  dataType, result)

# Exit QGIS
QgsApplication.exitQgis()
QApplication.exit()

Along with the output, the script creates TEMP files in the following location:

%USER%\AppData\Local\Temp\3\processinga47e029c49534bc895c55106582f3a25\4398ed40e94f4f3285977b396abc2268\E:outputtif.sgrd

How can I set a different path for the TEMP files?

3

You can overwrite the temporary folder where Processing writes files by calling os.environ before executing the algorithm (i.e., before calling processing.runalg()):

On Windows:

import os           # This line should go in the imports section
os.environ['TMP'] = "E:\\my\\own\\tmp\\folder\\"

On GNU/Linux:

import os              # This line should go in the imports section
os.environ['TMPDIR'] = "/my/own/tmp/folder/"

The good part is that using os.environ you won't set the environment variable TEMP/TMP/TMPDIR permanently. It works for the current Python script/application, so you won't mess other applications up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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