This answer assumes that you have an OSGeo4W64 installation of QGIS. If you have a standalone install, you will need to alter some of the files in the batch file and Python script.
First, create and save a batch file with the following content. As I said, make sure all the paths are correct for your system and installation. This batch file will set up the required environment to run QGIS processing algorithms including Grass algs with Python outside of the QGIS Python console.
@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\bin\qt5_env.bat
call "%OSGEO4W_ROOT%"\bin\py3_env.bat
call "%OSGEO4W_ROOT%"\apps\grass\grass78\etc\env.bat
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;C:\OSGeo4W64\apps\Qt5\bin
path %PATH%;C:\OSGeo4W64\apps\Python37\Scripts
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
cmd.exe
This line call "%OSGEO4W_ROOT%"\apps\grass\grass78\etc\env.bat
is required to run Grass algorithms from a standalone script. Give the batch file a logical name (e.g. qgis-python-grass.bat).
Save the following script as a .py
file. Again, it is up to you change all paths in this script to match your system and installation including the paths to your input and output .tif files.
import sys
from qgis.core import QgsApplication
#from qgis.analysis import QgsNativeAlgorithms
QgsApplication.setPrefixPath('C:/OSGeo4W64/apps/qgis', True)
qgs = QgsApplication([], False)
qgs.initQgis()
sys.path.append('C:\\OSGeo4W64\\apps\\qgis\\python\\plugins')
import processing
from processing.core.Processing import Processing
Processing.initialize()
#QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
alg_params = {
'map':'C:/Users/Path/To/Input.tif',
'setnull':'',
'null':0,
'-f':False,
'-i':False,
'-n':False,
'-c':False,
'-r':False,
'output':'C:/Users/Path/To/Output.tif',
'GRASS_REGION_PARAMETER':None,
'GRASS_REGION_CELLSIZE_PARAMETER':0,
'GRASS_RASTER_FORMAT_OPT':'',
'GRASS_RASTER_FORMAT_META':''
}
processing.run("grass7:r.null", alg_params)
For argument's sake, let's say that we will call this script r-null_standalone.py
. Place both this script and the batch file in the same folder. You can then simply double click the batch file to launch a cmd window. At the command prompt just type python3 r-null_standalone.py
and hit Enter to run your Python script.
Update Jan 2022
For newer versions of QGIS installed with the new OSGeo4W installer, the batch file looks slightly different. I used the following batch file to set the environment then retested this answer using the script and steps described above. It worked fine for me in QGIS 3.20.3.
@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%\apps\grass\grass78\etc\env.bat"
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;C:\OSGeo4W\apps\Qt5\bin
path %PATH%;C:\OSGeo4W\apps\Python39\Scripts
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python39
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
cmd.exe