6

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

-- 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()
  • Try replacing app = QApplication([]) with app = QgsApplication([],False, None) and QgsApplication.exitQgis() with app.exit(). – Joseph Jan 14 '16 at 12:40
  • @Joseph thank for advice but it raise this error : Traceback (most recent call last): File "/home/qsuser/.qgis2/python/plugins/ocs_qgs/test.py", line 29, in <module> app = QgsApplication([], False, None) TypeError: QgsApplication(list, bool, QString customConfigPath=QString()): argument 3 has unexpected type 'NoneType' – SIGIS Jan 14 '16 at 12:56
  • I knew I've seen a somewhat similar problem from another post. Perhaps this might give you some hints? – Joseph Jan 14 '16 at 13:01
  • I follow the the steps found in that post. It works in the console python of terminal. but not in pycharm. Maybe it relate to Pycharm ? – SIGIS Jan 14 '16 at 14:35
  • Could be, I'm not experienced enough in this so hopefully others can advise :) – Joseph Jan 14 '16 at 14:41
3

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.

  • Yes ! it works ! The problem was the order as you said @Matthias Kuhn ! thanks for the help and the explanation ! – SIGIS Jan 15 '16 at 10:53
  • As an alternative to importing qgis, the SIP API version can be set directly: stackoverflow.com/questions/6238193/pyqt-new-api-with-python-2 – Kris Jul 13 '16 at 17:02
  • Don't!! There are other things done as well in the qgis import and especially with the upcoming version of QGIS 3 you are better of if you always import qgis. Is there a reason why you would not want to import qgis? – Matthias Kuhn Jul 13 '16 at 17:08

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.