In Windows 10, I currently have two working scripts (.BAT and .PY) which are used to generate PDFs from QGIS based on the solution here.
My goal is to be able to execute the python script without having to call the batch commands each time, but am having trouble getting this to work.
The createQGISmap.bat
file contains:
REM Change OSGeo4W_ROOT to point to your install of QGIS.
SET OSGEO4W_ROOT=C:\Program Files\QGIS 2.18
SET QGISNAME=qgis
SET QGIS=%OSGEO4W_ROOT%\apps\%QGISNAME%
set QGIS_PREFIX_PATH=%QGIS%
CALL "%OSGEO4W_ROOT%\bin\o4w_env.bat"
REM Python Setup
set PATH=%OSGEO4W_ROOT%\bin;%QGIS%\bin;%PATH%
SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python27
set PYTHONPATH=%QGIS%\python;%PYTHONPATH%
ECHO OSGeo path is: %OSGEO4W_ROOT%
ECHO Getting QGIS libs from: %QGIS%
ECHO Python loaded from: %PYTHONHOME%
"C:\Program Files\QGIS 2.18\bin\python.exe" getScreenshot.py
And the beginning of the getScreenshot.py
script:
from qgis.core import *
import sys
from qgis.gui import QgsMapCanvas, QgsLayerTreeMapCanvasBridge
from PyQt4.QtCore import QFileInfo, QObject
from PyQt4.QtXml import QDomDocument
gui_flag = True
app = QgsApplication(sys.argv, gui_flag)
# Make sure QGIS_PREFIX_PATH is set in your env if needed!
app.initQgis()
# project.qgs
project_path = 'C:/mydata/qgis/autoexport.qgs'
I tried manually specifying the environment variables in Windows (in C: - Properties - Advanced system settings - Environment variables...) as follows:
QGIS = C:\Program Files\QGIS 2.18\apps\qgis
QGIS_PREFIX_PATH = C:\Program Files\QGIS 2.18\apps\qgis
PATH = C:\Program Files\QGIS 2.18\bin
PATH = C:\Program Files\QGIS 2.18\apps\qgis\bin
PYTHONHOME = C:\Program Files\QGIS 2.18\apps\Python27
PYTHONPATH = C:\Program Files\QGIS 2.18\python
But when executing python getScreenshot.py
from the OSGeo4W shell, I keep getting
from qgis.core import *
ImportError: No module named qgis.core
I tried following advice from How can I fix "ImportError: No module named qgis.core"? and here without luck. I should point out I have two installations of Python (the one bundled with QGIS and a Python 3 from Anaconda), so I am trying to run the script with the former installation.