1

I am trying to get PyQGIS (3.4.6) to work in a standalone script on Windows 10 - preferably in PyCharm, though not necessarily. A solution where I can externally call a python script with initialized Qgis would also work.

If I alter python-qgis-ltr.bat in a way to reflect paths etc it'll work like a charm: (It would be Okish to call python scripts from the console - but I dont wanna paste them into the console and if I open a new console the path variables are gone)

@echo off
SET QGIS_ROOT=C:\Programme\QGIS 3.4
call "%QGIS_ROOT%"\bin\o4w_env.bat
call "%QGIS_ROOT%"\bin\qt5_env.bat
call "%QGIS_ROOT%"\bin\py3_env.bat
@echo off
path %QGIS_ROOT%\bin;%PATH%
path %QGIS_ROOT%\apps\qgis-ltr\bin;%PATH%
set QGIS_PREFIX_PATH=%QGIS_ROOT%\apps\qgis-ltr
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%QGIS_ROOT%\apps\qgis- 
ltr\qtplugins;%QGIS_ROOT%\apps\qt5\plugins
set PYTHONPATH=%QGIS_ROOT%\apps\qgis-ltr\python;%PYTHONPATH%
"%PYTHONHOME%\python" %*

If I do basically the same for PyCharm an import of qgis.core results in this error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\PyCharm Community Edition 
 2019.1.2\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in 
do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Programme\QGIS 3.4\apps\qgis-ltr\python\qgis\core\__init__.py", 
line 27, in <module>
    from qgis._core import *
  File "C:\Program Files\PyCharm Community Edition 
2019.1.2\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in 
do_import
    module = self._system_import(name, *args, **kwargs)
ImportError: DLL load failed: The specified module could not be found.

Code for PyCharm initialization:

@echo off
SET QGIS_ROOT=C:\Programme\QGIS 3.4
call "%QGIS_ROOT%"\bin\o4w_env.bat
call "%QGIS_ROOT%"\bin\qt5_env.bat
call "%QGIS_ROOT%"\bin\py3_env.bat
@echo off
path %QGIS_ROOT%\bin;%PATH%
path %QGIS_ROOT%\apps\qgis-ltr\bin;%PATH%
set QGIS_PREFIX_PATH=%QGIS_ROOT%\apps\qgis-ltr
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%QGIS_ROOT%\apps\qgis-ltr\qtplugins;%QGIS_ROOT%\apps\qt5\plugins
set PYTHONPATH=%QGIS_ROOT%\apps\qgis-ltr\python;%QGIS_ROOT%\apps\qgis-ltr\python\plugins;%QGIS_ROOT%\apps\qgis-ltr\python\qgis;%PYTHONPATH%
start "PyCharm aware of Quantum GIS" /B "C:\Program Files\PyCharm Community Edition 2019.1.2\bin\pycharm64.exe" %*

Note that in PyCharm the QGIS Python 3.7 executeable is defined as the Python interpreter and that every path returned from sys.path in the python console is also returned in PyCharm. I don't know where to search for errors anymore... Also note that I tried incorporating the code from Why does QGIS 3.2 "native:extractvertices" algorithm not work properly in standalone script? without success. I installed without OSGEO4W - shouldn't make a difference though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.