Unable to run standalone PyQGIS script with launch.bat file

batchenvironment variablepyqgisstandalone

https://www.qgistutorials.com/ru/docs/running_qgis_jobs.html

I want to run a script in a file called my_plugin.py.
The script is:

print ('Hello QGIS!')

The path for the script is:

C:\OSGeo4W\apps\Python39\my_plugin.py

if I open my command prompt and I do:

C:\OSGeo4W\apps\Python39>python my_plugin.py

But I want one that works with a bat file!!

In the tutorial, it is written in page 14 to set the environment in the bat file as follows:

REM Change OSGEO4W_ROOT to point to the base install folder 
SET OSGEO4W_ROOT=C:\OSGeo4W64 SET QGISNAME=qgis 
SET QGIS=%OSGEO4W_ROOT%\apps\%QGISNAME% set QGIS_PREFIX_PATH=%QGIS% 
REM Gdal Setup set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal\ 
REM Python Setup
set PATH=%OSGEO4W_ROOT%\bin;%QGIS%\bin;%PATH% 
SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python27 
set PYTHONPATH=%QGIS%\python;%PYTHONPATH%
    
REM Launch python job 
python c:\Users\Ujaval\Desktop\download_and_clean.py 
pause

Obviously, I have adapted the suggested environments in the tutorial intuitively with my personal path created with OSGEOW4 QGIS 3.22.14 and Python39. I've written a file launch.txt with the following environment variables:

REM Change OSGEO4W_ROOT to point to the base install folder
SET OSGEO4W_ROOT=C:\OSGeo4W
SET QGISNAME=qgis-ltr
SET QGIS=%OSGEO4W_ROOT%\apps\%QGISNAME%
set QGIS_PREFIX_PATH=%QGIS%
REM Gdal Setup
set GDAL_DATA=%OSGEO4W_ROOT%\apps\gdal\share\gdal\
REM Python Setup
set PATH=%OSGEO4W_ROOT%\bin;%QGIS%\bin;%PATH%
SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python39
set PYTHONPATH=%QGIS%\python;%PYTHONPATH%

REM Launch python job
python C:\OSGeo4W\apps\Python39\my_plugin.py
pause

After that, I change the extension of the file launch.txt to launch.bat.

I double click the launch.bat file. It runs but it stops before writing

Hello QGIS!

Here is the image after running the launch.bat file. I also have checked and every path is ok redirecting to an existing path, but it does not work.
cmd terminal screenshot

What do I have to do to solve this issue with the bat file?

EDIT 26.12.2022
Now it works.

I found errors or things that in my case prevent obtaining the result.

1.I had to set my environment variable in Environment Variables in the properties: https://docs.oracle.com/en/database/oracle/machine-learning/oml4r/1.5.1/oread/creating-and-modifying-environment-variables-on-windows.html

2.here is the content of my launch.bat file. It is deprived of all environment variable and there is only the final part.

ECHO OFF
C:\OSGeo4W\apps\Python39\python.exe C:\Users\Simone\OneDrive\Desktop\my_plugin.py
PAUSE

this final part is different of my old reference that had written only python…..pause instead of ECHO OFF….pause:

REM Launch python job
python c:\Users\Ujaval\Desktop\download_and_clean.py
pause
  1. my reference had written print 'Hello QGIS!'instead of print ('Hello QGIS!')

Best Answer

At least for Windows, that QGIS tutorial makes a lot more work out of running a QGIS Python script from a batch file than is necessary. Why create a batch file to set all the environment variables when QGIS has already done the work with qgis_process-qgis.bat? I would argue it is better to rely on QGIS setting the Windows environment variables than trying to manually set them.

Contents of launch.bat:

rem Call qgis_process-qgis.bat file to set QGIS Windows environment variables
call "C:\Program Files\QGIS 3.28.0\bin\qgis_process-qgis.bat"

rem Append QGIS Python Plugins directory to PYTHONPATH
set PYTHONPATH=%PYTHONPATH%;%QGIS_PREFIX_PATH%/python/plugins

rem Launch Python job
python c:\Users\Ujaval\Desktop\download_and_clean.py

rem Pause Windows batch job
pause

You will have to modify the path based on the QGIS install on your local machine.

UPDATE: I updated the code above to reflect the fact that calling o4w_env.bat sets everything up to use the OSGeo4W bundled Python interpreter, but it doesn't set everything up to use qgis Python package(s). Calling qgis_process-qgis.bat calls o4w_env.bat, but it adds settings to use qgis Python packages. However, that does not address the matter of QGIS Python plugins, so the plugin directory needs to be appended to the PYTHONPATH environment setting.

Related Question