3

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:

enter image description here

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:

enter image description here

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:

enter image description here

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?

3

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.

enter image description here

  • That works, thanks, even though I don't fully grasp what's happening. – HDR Jan 13 at 12:01
1

Could you try something like this for python file?

C:\PROGRA~1\QGIS3~1.4\apps\Python37\python.exe c:/Users/user/Desktop/xx/run.py 

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.