2

I installed QGIS 3.10 on Win10-64 and I try to use the qgis core and processing functionalities from PyCharm IDE. I used to do this successfully with previous QGIS versions, but this time the output puzzles me.

I initiate my batch file like this:

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%\bin\qt5_env.bat"
call "%OSGEO4W_ROOT%\bin\py3_env.bat"
call "%OSGEO4W_ROOT%"\apps\grass\grass78\etc\env.bat
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass78\lib
path %PATH%;%OSGEO4W_ROOT%\apps\Qt5\bin
path %PATH%;%OSGEO4W_ROOT%\apps\Python37\Scripts
path %PATH%;C:\qgis-plugins\qgis-otb-plugin
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\python\plugins
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\python\plugins\processing
path %PATH%;C:\Users\...\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins
path %PATH%;%OSGEO4W_ROOT%\apps\saga
path %PATH%;%OSGEO4W_ROOT%\apps\gdal2

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=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37

set QT_QPA_PLATFORM_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\Qt5\plugins\platforms
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis

start "PyCharm aware of Quantum GIS" /B "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3\bin\pycharm64.exe" %*

cmd.exe

Then I try with this little test script:

import os
import sys

from qgis.core import *

for alg in QgsApplication.processingRegistry().algorithms():
        print(alg.id(), "->", alg.displayName())

Which returns

Application path not initialized

Application path not initialized

Process finished with exit code 0

I have built the project interpreter (virtual env) through the base interpreter "C:\OSGeo4W64\apps\Python37\python.exe" inheriting global site packages.

Just a heads up, the cmd gives the following PATH appended variables:

C:\Windows\System32>echo %PATH:;=&echo.%
C:\OSGEO4~1\apps\Python37
C:\OSGEO4~1\apps\Python37\Scripts
C:\OSGEO4~1\apps\Python37
C:\OSGEO4~1\apps\Python37\Scripts
C:\OSGEO4~1\apps\qt5\bin
{app}
C:\OSGEO4~1\apps\Python27\Scripts
C:\OSGEO4~1\bin
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\system32\WBem
C:\OSGEO4~1\apps\msys\bin
C:\Program Files\R\R-3.5.1\bin\x64
C:\Program Files\RStudio\bin
C:\OSGEO4~1\apps\qgis\bin
C:\OSGEO4~1\apps\grass\grass78\lib
C:\OSGEO4~1\apps\Qt5\bin
C:\OSGEO4~1\apps\Python37\Scripts
C:\qgis-plugins\qgis-otb-plugin
C:\OSGEO4~1\apps\qgis\python\plugins
C:\OSGEO4~1\apps\qgis\python\plugins\processing
C:\Users\...\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins
C:\OSGEO4~1\apps\saga
C:\OSGEO4~1\apps\gdal2

2 Answers 2

6

For standalone Python script, you have to set QGIS Environment path. Try to add below code first:

from qgis.core import *
# Initialize QGIS Application
qgs = QgsApplication([], False)
QgsApplication.setPrefixPath("C:\OSGEO4~1\apps\qgis", True)
QgsApplication.initQgis()
for alg in QgsApplication.processingRegistry().algorithms():
        print(alg.id(), "->", alg.displayName())
2
0

This is the reference. There a whole manual about standalone scripts using pyqgis and qgis.core there. https://docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/intro.html

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.