3

Ultimately I'm attempting to launch the QGIS GUI and run a python script from a batch file. I've reviewed numerous instances referencing similar scenarios on this and other forums but for the life of me I can't get it to work.

My batch file code is:

@echo off

SET OSGEO4W_ROOT=C:\Program Files\QGIS 3.4

call "C:\Program Files\QGIS 3.4\bin\o4w_env.bat"
call "C:\Program Files\QGIS 3.4\bin\qt5_env.bat"
call "C:\Program Files\QGIS 3.4\bin\py3_env.bat"
@echo off
path %OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
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 QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%

python C:\temp\test.py
pause

My python script is:

import sys
from qgis.core import *

# Initialize QGIS Application
QgsApplication.setPrefixPath("C:\Program Files\QGIS 3.4\apps\qgis", True)
app = QgsApplication([], True)
QgsApplication.initQgis()

# Add the path to Processing framework
sys.path.append('C:\Program Files\QGIS 3.4\apps\qgis\python\plugins')

# Import and initialize Processing framework
from processing.core.Processing import Processing
Processing.initialize()
import processing

print('Hello QGIS!')

Currently this code fails with the following message: enter image description here

If I remove the importing of the processing framework section of the py script then 'Hello QGIS!' is printed in the batch window but the GUI does not launch.

Where am I going wrong?

2

Add an example that is working. It is very similar to yours but look at the \ and / (double slash and backslash).

It is important that they're the same as in my example. If it isn't so and I remember correctly it will not work for you.

test_processing.bat

@ECHO OFF 

set OSGEO4W_ROOT=D:\OSGeo4W64

@echo off
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
call "%OSGEO4W_ROOT%\bin\qt5_env.bat"
call "%OSGEO4W_ROOT%\bin\py3_env.bat"

@echo off
path %OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
set GDAL_FILENAME_IS_UTF8=YES

set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%

cd /d %~dp0

python3 test.py

pause

test.py

import sys
import os
import logging

# Append the path where processing plugin can be found
sys.path.append('D:\\OSGeo4W64\\apps\\qgis\\python\\plugins')

from qgis.core import (
    QgsApplication,
    QgsProcessingFeedback,
    QgsVectorLayer,
    QgsProject
)

from qgis.core import QgsApplication

QgsApplication.setPrefixPath('D:/OSGeo4W64/apps/qgis', True)
qgs = QgsApplication([], True)
qgs.initQgis()

import processing
from processing.core.Processing import Processing
Processing.initialize()

print("Finished importing")

processing.algorithmHelp("qgis:zonalstatistics")

QgsApplication.exitQgis()

Console result:

enter image description here

I hope this helps you

  • Thanks Fran, that seems to have fixed the loading processing problem although i do get several DepricationWarnings regarding invalid escape sequences. However the GUI still does not launch? does it launch for you? – kiwibyron Jul 25 at 21:03
  • This doesnt launch the interface, it allows you to use interface functionalities but such scripts do not "open" QGIS or open UI – Fran Raga Jul 26 at 6:17
1

For anyone else that stumbles across this question, I managed to solve the issue using a batch file with the following code:

cd C:\Program Files\QGIS 3.4\bin

qgis --project "path to .qgz project file" --code "path to .py python code file"

The qgis bit in the second line needs to match whatever .bat file is run to open QGIS. For some users this was qgis-ltr. This launches QGIS and opens a saved project, then runs the python script.

And my python script needed to start with the following to execute properly:

from qgis.core import *
import qgis.utils
from qgis.utils import iface

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.