2

I have been following various posts and tutorials including Anita Graser's posts Anita Graser and https://gis.stackexchange.com/questions/302774/ as I try to set up my PyCharm installation to access and work with the QGIS libraries in my OSGeo4W64 QGIS 3.14 installation

I have created the pygis.cmd and pycharm.cmd files that are required and they appear to do what they should. My pycharm.cmd successfully launches 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" 

However, despite following https://gis.stackexchange.com/questions/302774/ I continue to get this error message popping up

enter image description here

I am sure that I have set my Qt Plugin Path.correctly which post 302774 advises to include:

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

in the pycharm.cmd file

Can anyone offer some advice on where I might be going wrong? I'm hoping 'Ben W' amongst others may see this question as his advice helped others in the 302774 post but it's not working for me in 2020. So perhaps there are other anomalies in the set up now required for later versions of QGIS?

1
  • I would ordinarily ask this as a separate question but as it seems to be related .. As I continue to work on my problem here I have encountered another error message when I try to run some parts of a test script -- proj_create_from_database: Cannot find proj.db -- I have seen a post about setting an environment variable PROJ_LIB=C:\OSGeo4W64\share\proj to solve this which I have added to the cmd files. Is that an appropriate approach?
    – TrevP
    Commented Aug 15, 2020 at 14:48

1 Answer 1

2

Your Qt plugin path looks OK. Below is the batch file which I am currently using. I have tested this with a newly installed QGIS 3.14.15 (OSGeo4W install) and I'm not getting any errors. You could try it and see if it works for you.

*Note: I have been using PyScripter rather than PyCharm for a while now, but it really shouldn't make any difference. Just change the last line to start PyCharm instead.

@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"

Starting PyScripter with this batch file, I can run the following with no errors.

To test QGIS libraries:

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

And to test 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()
2
  • Thank you for this. Both a little confused and happy because this seems to be working. Only noticeable differences are your lines setting the prefix path up to and including the VSI_CACHE lines. I had made some progress myself but was stuck on another error where proj.db couldn't be found. With your code, this too seems to have been resolved. Thanks very much. I can now carry on and try and get a few processing algorithms to work. Not expecting a smooth ride bit much further forward now. Cheers
    – TrevP
    Commented Aug 16, 2020 at 11:55
  • No worries @TrevP, glad it's working. I'm certainly no batch file guru, but the basis for the files I use are batch files found in the OSGeo4W64/bin folder. These are called python-qgis.bat, python-qgis-ltr.bat or python-qgis-dev.bat depending on your install. The method of simply copying & renaming these files and changing the last to start an IDE has never worked for me, but I do copy the bulk of them. Whenever I upgrade to a new version, I always inspect these batch files for any changes, and edit my batch files accordingly.
    – Ben W
    Commented Aug 17, 2020 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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