I'm writing a simple Python Geoprocessing script which leverages two specific modules:
from osgeo import gdal
import pyproj
The script must be shared with colleagues in my organisation.To avoid hooking them up with a new Python installation, I intend to leverage the QGIS python interpreter which has these two modules by default (using QGIS 3.4.3 Madeira on W10). See image below:
I'm having difficulties finding and using QGIS's Python interpreter from which my colleagues could run my script. Firstly, the normal method for identifying the interpreter doesn't point to a Python interpeter but to qgis-bin.exe instead:
Secondly, the most obvious candidate for the Python interpeter (located at c:\program files\qgis3.4\apps\Python37\python.exe
) doesn't recognize the same modules:
To summarize: using Python from within QGIS provides the right modules, but using the (presumably) same Python interpreter outside QGIS fails to import the same modules. How can this be?
Answer
You need to set the environment variables to run the QGIS python outside.
In this case my QGIS installation is in D:\QGIS, so you should only change OSGEO4W_ROOT.
@ECHO OFF
set OSGEO4W_ROOT=D:\QGIS
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
call "%OSGEO4W_ROOT%\bin\qt5_env.bat"
call "%OSGEO4W_ROOT%\bin\py3_env.bat"
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
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
set PYTHONPATH=%OSGEO4W_ROOT%\apps\Python37\lib\site-packages;%PYTHONPATH%
set QT_QPA_PLATFORM_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\Qt5\plugins\platforms
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis
cd /d %~dp0
call cmd
and in the console you can import modules now.
No comments:
Post a Comment