Saturday 17 March 2018

qgis - PyQGIS couldn't import processing with standalone script


I have read post on that problem (How can I access `processing` with Python?). The problem is even with a sys.path.append it couldn't import processing. It's very frustrated. Here the code I used:


#!/usr/bin/python


def main():
layer = QgsVectorLayer('/path/to/file.shp', "BAT","ogr")
QgsMapLayerRegistry.instance().addMapLayers([layer])

# Testing processing
try :
Processing.runalg("qgis:creategrid")
except:
print "Processing module has not been loaded"


#Check the QgsMapLayerRegistry for the layers
print QgsMapLayerRegistry.instance().mapLayers()
# Check if they are valid
print layer.isValid()

if __name__=="__main__":
import sys
from PyQt4.QtGui import *
from qgis.core import *

app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

sys.path.append('home/qsuser/.qgis2/python/plugins')

# try to load processing
try:
from processing.core.Processing import Processing
Processing.initialize()


except:
print "unable to load processing"

main()

QgsApplication.exitQgis()

I could load QGIS and layer is valid that's ok, but processing resist to me. I am on Ubuntu 14.04 I use Pycharm to do it. I tried on terminal with the python console, it raised this error:


Traceback (most recent call last):

File "", line 1, in
File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 572, in _import
mod = _builtin_import(name, globals, locals, fromlist, level)
File "/home/qsuser/.qgis2/python/plugins/processing/__init__.py", line 28, in
from processing.tools.dataobjects import *
File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 572, in _import
mod = _builtin_import(name, globals, locals, fromlist, level)
File "/home/qsuser/.qgis2/python/plugins/processing/tools/dataobjects.py", line 35, in
from processing.core.ProcessingConfig import ProcessingConfig
File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 572, in _import

mod = _builtin_import(name, globals, locals, fromlist, level)
File "/home/qsuser/.qgis2/python/plugins/processing/core/ProcessingConfig.py", line 30, in
from PyQt4.QtCore import QPyNullVariant, QCoreApplication, QSettings
ImportError: cannot import name QPyNullVariant

I don't know what I am missing.


Settings :


Qgis 2.12.1-Lyon
Ubuntu 14.04 Trusty
IDE : Pycharm 5 community

Processing : 2.12.2

-- Edit -- And with the changes suggest by mathias kuhn


#!/usr/bin/python

def main():
layer = QgsVectorLayer('/path/to/file.shp', "BAT","ogr")
QgsMapLayerRegistry.instance().addMapLayers([layer])

# Testing processing

try :
Processing.runalg("qgis:creategrid")
except:
print "Processing module has not been loaded"

#Check the QgsMapLayerRegistry for the layers
print QgsMapLayerRegistry.instance().mapLayers()
# Check if they are valid
print layer.isValid()


if __name__=="__main__":
import sys
from qgis.core import *
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

sys.path.append('home/user/.qgis2/python/plugins')


# try to load processing
try:
from processing.core.Processing import Processing
Processing.initialize()

except:
print "unable to load processing"

main()


QgsApplication.exitQgis()

Answer



This error


ImportError: cannot import name QPyNullVariant

It indicates that the SIP API version is not set to version 2. When you write standalone python scripts, make sure that the very first import is


import qgis

The SIP API Version always needs to be set before any other PyQt code is executed. The qgis import takes care of that.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...