3

So, I have this little script (which I would like to build more upon).

I was able to make it work in QGIS3.2 (just change all paths and environments to 3.2) but now, after deleting QGIS 3.2 and installing 3.4 or 3.6 it does not work anymore.

I am able to start QgsApplication. But after testing, isValidLayer(), the result is false. So any processing creates empty output.

Environments and python is set up only for QGIS3.6 (Python37). If I install QGIS 3.2 and set it up back, all works just fine. But i am not able to change it to version 3.6. It is driving me crazy!

Any ideas?

import os, sys, datetime, glob, re, pprint
from qgis.core import *

from qgis.analysis import QgsNativeAlgorithms

sys.path.append(r'c:/SW/QGIS3.6/apps/qgis/python/plugins')

#-------------------------------------------------------------
#  start QGIS processing application

QgsApplication.setPrefixPath(r'C:/SW/QGIS3.6/apps/qgis', True)
print(QgsApplication.prefixPath())
qgs = QgsApplication([], False)
qgs.initQgis()

from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
#-------------------------------------------------------------


# folder = r'd:\01_Projects'
# files = glob.glob(folder + '/**/BUDOVY_P.shp', recursive=True)

out = r''
file = r'd:\01_Projects\BUDOVY_P.shp'


layer = QgsVectorLayer(file, 'tmp', 'ogr')
print(layer.isValid())
qgs.exitQgis()

Results is:

C:/SW/QGIS3.6/apps/qgis
False

pth file in site-packages for setting up PyCharm:

C:\SW\QGIS3.6\apps\qgis\python
C:\SW\QGIS3.6\apps\Python37\
C:\SW\QGIS3.6\apps\Python37\Lib\
C:\SW\QGIS3.6\apps\Python37\Lib\site-packages
C:\SW\QGIS3.6\bin
C:\SW\QGIS3.6\include
C:\SW\QGIS3.6\apps\qgis\bin
C:\SW\QGIS3.6\apps\qgis\python\plugins
C:\SW\QGIS3.6\apps\grass\grass-7.6.0
C:\SW\QGIS3.6\apps\Qt5\bin

env variables:

C:\SW\QGIS3.6\apps\qgis\bin
C:\SW\QGIS3.6\bin
C:\SW\QGIS3.6\apps\Python37\Scripts

The same part run in QGIS returns:

Python Console
Use iface to access QGIS API interface or Type help(iface) for more info
Security warning: typing commands from an untrusted source can lead to data loss and/or leak
file = r'd:/01_Projects/BUDOVY_P.shp'
layer = QgsVectorLayer(file, 'tmp_lyr', 'ogr')
print(layer.isValid())
True
  • Can't test it right now, but can you check whether it makes any difference that you are using a backslash in the script in the filename and a forward slash in the Python console run? – Houska Mar 19 at 9:58
  • I thought of something like that too. I changed all of the / to \ and still same thing. I also changed the literall string to unicode and still the same. But where would this change come from? Between Python 3.6 and 3.7? As i said, it worked just fine on QGIS 3.2 which had Python 3.6. To me it seems like something inside Python has chaged, because I have no clue what else – Jan Doležal Mar 19 at 10:05
  • I also found a similar error here: issues.qgis.org/issues/21195 But their solution does not work for me. Or maybe i am not able to change the solution from Mac to Windows – Jan Doležal Mar 19 at 10:07
  • possible duplicated gis.stackexchange.com/a/289964/49538 – Fran Raga Mar 19 at 16:33
  • I do not think it is a duplicate, because my part is working with QGIS 3.2. Which is their version. I am also not using any bat files and i do not want that. since it is on many computers and I need an easy and simple one time setup. – Jan Doležal Mar 20 at 8:17
1

After many hours of trial and error I found a solution (sort of). I hat to manually set environment variable for QGIS_PREFIX_PATH. But after this, I got a warning, there is a problem with gdal_data, and it can not find the gcv.csv file or something like that. Which is kind of funny, because I have it set up:

GDAL_DATA
C:\SW\QGIS3.6\share\gdal

But nevermind, QGIS can't see it even though printing it with os.environ['GDAL_DATA'] works just fine.

And with setting up the QGIS_PREFIX_PATH, you do not have to:

QgsApplication.setPrefixPath()

As you can see it is very funny problem, so the final code is:

import os, sys, datetime, glob, re, pprint
from qgis.core import *

from qgis.analysis import QgsNativeAlgorithms
sys.path.append(r'c:\SW\QGIS3.6\apps\qgis\python\plugins')
#-------------------------------------------------------------
#  start QGIS processing application
os.environ['QGIS_PREFIX_PATH'] = r'C:\SW\QGIS3.6\apps\qgis'
os.environ['GDAL_DATA'] = r'C:\SW\QGIS3.6\share\gdal'

qgs = QgsApplication([], False)
QgsApplication.initQgis()

import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
# -------------------------------------------------------------
file = r'd:\01_Projects\BUDOVY_P.shp'


layer = QgsVectorLayer(file, 'tmp', 'ogr')
print(layer.isValid())

>>True

If I add QGIS_PREFIX_PATH to env variables in windows so it is permanently there. it does not work. I have to set it manually in script. Yeah well...

I also tried calling QApplication instead of QgsApplication and I managed it to work but without additional processing. If I try to call processing module, Python crashes completely. So there was a no go.

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.