1

I have installed QGIS 2.18, QGIS 3.8 and OSGeo on a Windows machine.

I tried to set up a PyCharm environment for PyQGIS coding, but I can't initialize the OGR provider to import shapefiles.

Here's the code:

qgs = QgsApplication([], False)
qgs.setPrefixPath(r'C:/PROGRA~1/QGIS3~1.8/apps/qgis', True)
qgs.initQgis()

When I try to run

qgs = QgsApplication.setPrefixPath(r'C:/PROGRA~1/QGIS3~1.8/apps/qgis', True)
print(qgs)

It returns None. If I have installed three versions of QGIS in Windows there are some problems with prefixPath()?

  • 1
    Try defining the full path instead of the shorthand. – Joseph Sep 25 '19 at 11:28
  • I've used the full path, is there another way to load the providers? In linux i use '/usr' as prefix path and it is right. – nicola frosi Sep 25 '19 at 16:26
  • Now i have uninstalled all QGIS version and installed only QGIS3.8, but the problem is not solved. Did you mean using os.path.abspath(..)? – nicola frosi Sep 25 '19 at 16:33
  • I'll try tomorrow on windows, thank you. – nicola frosi Sep 25 '19 at 16:40
  • 1
    I have solved all the settings problems, thank you – nicola frosi Sep 26 '19 at 13:41
1

Here's the right batch file. It works for me, i use windows10, PyCharm Community Edition 2019.2.2 and OSGEO-QGIS3.8.

SET OSGEO4W_ROOT=C:\OSGeo4W64
SET QGISNAME=qgis
SET QGIS=%OSGEO4W_ROOT%\apps\%QGISNAME%
SET QGIS_PREFIX_PATH=%QGIS%
SET PYCHARM="C:\Program Files\JetBrains\PyCharm Community Edition 2019.2.2\bin\pycharm64.exe"




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

set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis

call %OSGEO4w_ROOT%\bin\o4w_env.bat
call %OSGEO4w_ROOT%\bin\py3_env.bat
call "%OSGEO4W_ROOT%\bin\qt5_env.bat"

SET PATH=%PATH%;%QGIS%\bin
SET PYTHONPATH=%QGIS%\python;%PYTHONPATH%


@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass76\lib
path %PATH%;%OSGEO4W_ROOT%\apps\Qt5\bin
path %PATH%;%OSGEO4W_ROOT%\apps\Qt5\plugins
path %PATH%;%OSGEO4W_ROOT%\apps\Qt5\plugins
path %PATH%;%OSGEO4W_ROOT%\apps\Qt5\plugins\platforms
path %PATH%;%OSGEO4W_ROOT%\apps\qgis
path %PATH%;%OSGEO4W_ROOT%\bin

start "PyCharm aware of QGIS" /B %PYCHARM% %*
  • Ok, thank you i am a novice in the blog – nicola frosi Oct 1 '19 at 17:42

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.