I have read Accessing `processing` with Python?. The problem is even with a sys.path.append it couldn't import processing.
Here is 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 "<stdin>", line 1, in <module>
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 <module>
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 <module>
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 <module>
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
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()