0

I am executing a .py file from a windows .bat file for doing a QGIS Desktop 3.8.3 geoprocessing task.

I am using the following .bat file :

@echo off
call C:\OSGeo4W64\bin\o4w_env.bat
REM Change OSGEO4W_ROOT to point to the base install folder
set QGIS_PREFIX_PATH=C:\OSGeo4W64\apps\qgis
REM Gdal Setup
set GDAL_DATA=C:\OSGeo4W64\share\gdal\
REM Python Setup
set PATH=C:\OSGeo4W64\bin;C:\OSGeo4W64\apps\qgis\bin;%PATH%
SET PYTHONHOME=C:\OSGeo4W64\apps\Python37
set PYTHONPATH=C:\OSGeo4W64\apps\qgis\python;%PYTHONPATH%

REM Launch python job
python C:/Task/Task.py
pause

When I execute this Task.bat in C:/Task/, I am getting the following error.

  File "C:\OSGeo4W64\apps\Python37\lib\site.py", line 177
    file=sys.stderr)
        ^
SyntaxError: invalid syntax
Press any key to continue . . .

There are 2 python files namely Python27 and Python37 in C:\OSGeo4W64\apps\ .

The python-core.bat file in C:\OSGeo4W64\etc\ini\ is :

SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
PATH %OSGEO4W_ROOT%\apps\Python37\Scripts;%PATH%

When I open OSGeo4W.bat in C:\OSGeo4W64\ and type 'python', it is showing me SyntaxError and when I type 'python3', it is showing the version of python as given below.

run o-help for a list of available commands
C:\OSGeo4W64>python
  File "C:\OSGEO4~1\apps\Python37\lib\site.py", line 177
    file=sys.stderr)
        ^
SyntaxError: invalid syntax

C:\OSGeo4W64>python3
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit 
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

My python code C:/Task/Task.py starts as given below :

from osgeo import ogr, gdal
from gdalconst import *
from qgis.core import *
import qgis.utils, sys
from qgis.gui import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
from PyQt5.QtCore import QFileInfo
app = QApplication([])
QgsApplication.setPrefixPath("C:/OSGeo4W64/apps/qgis", True)
QgsApplication.initQgis()
sys.path.append('C:/OSGeo4W64/apps/qgis/python/plugins')
import processing
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *

How can I solve this SyntaxError for executing Task.bat in C:/Task/ ?

  • use python3 to run you task – Ian Turton Oct 16 '19 at 14:51
  • @IanTurton As you can see in my code, I am using python3 for this task. – Sreeraj Oct 16 '19 at 17:07
  • I don't think you are, please try it again with python3 – Ian Turton Oct 16 '19 at 17:36
  • 1
    call C:\OSGeo4W64\bin\py3_env.bat – user2856 Oct 16 '19 at 21:27
  • 1
    change python C:/Task/Task.py to python3 C:/Task/Task.py – Ian Turton Oct 17 '19 at 7:25
0

The solution to this issue is to change python C:/Task/Task.py to python3 C:/Task/Task.py in C:/Task/Task.bat, so that it is using python3 interpreter.

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.