Configurazione di PyCharm per lavorare con le librerie QGIS

Aug 15 2020

Ho seguito vari post e tutorial inclusi i post di Anita Graser, Anita Graser ehttps://gis.stackexchange.com/questions/302774/ mentre provo a configurare la mia installazione PyCharm per accedere e lavorare con le librerie QGIS nella mia installazione OSGeo4W64 QGIS 3.14

Ho creato i file pygis.cmd e pycharm.cmd che sono richiesti e sembrano fare quello che dovrebbero. Il mio pycharm.cmd avvia con successo PyCharm

> @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
rem path %PATH%;%OSGEO4W_ROOT%\apps\qgis
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass78\lib
path %PATH%;C:\OSGeo4W64\apps\Qt5\bin
rem path %PATH%;C:\OSGeo4W64\apps\qgis\python\plugins
path %PATH%;C:\OSGeo4W64\apps\Python37\Scripts
rem path %PATH%;C:\OSGeo4W\apps\Python37

set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37

set PATH=C:\Program Files\Git\bin;%PATH%
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\Qt5\plugins

start "PyCharm aware of QGIS" /B "C:\Users\b8060507\AppData\Local\JetBrains\PyCharm Community Edition 2019.3.3\bin\pycharm64.exe" 

Tuttavia, nonostante la seguente https://gis.stackexchange.com/questions/302774/ Continuo a ricevere questo messaggio di errore

Sono sicuro di aver impostato correttamente il mio percorso del plugin Qt. Che il post 302774 consiglia di includere:

set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins  

nel file pycharm.cmd

Qualcuno può offrire qualche consiglio su dove potrei sbagliare? Spero che "Ben W", tra gli altri, possa vedere questa domanda in quanto il suo consiglio ha aiutato gli altri nel post 302774 ma non funziona per me nel 2020. Quindi forse ci sono altre anomalie nella configurazione ora richieste per le versioni successive di QGIS?

Risposte

1 BenW Aug 16 2020 at 16:49

Il percorso del tuo plugin Qt sembra OK. Di seguito è riportato il file batch che sto utilizzando attualmente. L'ho testato con QGIS 3.14.15 appena installato (installazione OSGeo4W) e non ricevo alcun errore. Potresti provarlo e vedere se funziona per te.

* Nota: utilizzo PyScripter invece di PyCharm da un po 'di tempo, ma in realtà non dovrebbe fare alcuna differenza. Basta cambiare l'ultima riga per avviare invece PyCharm.

@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
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;C:\OSGeo4W64\apps\Qt5\bin
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
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 PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins

start "PyScripter aware of QGIS" /B "C:\Program Files\PyScripter\PyScripter.exe"

Avviando PyScripter con questo file batch, posso eseguire quanto segue senza errori.

Per testare le librerie QGIS:

from qgis.core import *
QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis", True)
qgs = QgsApplication([], False)
qgs.initQgis()
qgs.exitQgis()

E per testare PyQt:

from PyQt5.QtWidgets import QApplication, QMainWindow
import sys

class MyDialog(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        self.setGeometry(250, 250, 500, 350)

def main():
    app = QApplication(sys.argv)
    w = MyDialog()
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()