3

I've been trying to get standalone QGIS python scripting to work, but for some reason after I import processing, I cannot print anything to the console, nor can I write anything to file. My end goal is to create a library of standalone processing scripts, so being able to output information to the console is important to me.

I slightly altered the code from Error: algorithm not found, since the solution given gave me the error of "no module named processing".

My code is as follows:

#!/usr/bin/python
print "1"
import qgis
print "2"
from qgis.core import *
print "3"
from qgis.utils import *
utils.uninstallErrorHook() 
print "4"
app = qgis.core.QgsApplication([], True)
print "5"
import processing
print "6"

class DummyInterface(object):
    def __init__(self):
        self.destCrs = None
    def __getattr__(self, *args, **kwargs):
        def dummy(*args, **kwargs):
            return DummyInterface()
        return dummy
    def __iter__(self):
        return self
    def next(self):
        raise StopIteration
    def layers(self):
        # simulate iface.legendInterface().layers()
        return qgis.core.QgsMapLayerRegistry.instance().mapLayers().values()
iface = DummyInterface()
plugin = processing.classFactory(iface)

f = open("/home/alex/output.txt", "a")

a_list = processing.alglist()
for item in a_list:
    f.write(str(item))

f.write(len(a_list))

f.close()

When run from the terminal, this is what I get:

alex@blackbook-bear: ~
$ python test.py
1
2
3
4
5
alex@blackbook-bear: ~
$ cat /home/alex/output.txt
cat: /home/alex/output.txt: No such file or directory
alex@blackbook-bear: ~
$ 
4
  • How are you running this script? It's acting like import processing is raising an exception, but something is hiding the stderr output.
    – larsks
    Commented Dec 2, 2015 at 2:46
  • @larsks I'm executing it from a terminal. I'm running Linux on my machine. Commented Dec 2, 2015 at 2:49
  • Sorry, that was a stupid question caused by reading too quickly :).
    – larsks
    Commented Dec 2, 2015 at 2:58
  • @larsks I was able to solve it. I've posted my solution as a follow up. You were correct, there was something hiding stderr - I just wasn't disabling the thing that hides it properly. Commented Dec 2, 2015 at 5:20

1 Answer 1

1

Managed to solve the issue.

First off, I had the uninstallErrorHook() being used wrong. Instead of

utils.uninstallErrorHook() 

I should have used

uninstallErrorHook() 

Next, I had the wrong version of the processing libs downloaded. The version I had was incompatible with QGIS 2.8, but the errors it was throwing were invisible to me. This was solved by pointing to the correct location, which was /usr/share/qgis/python/plugins. Found that by using the builtin Python console in QGIS and entering

import sys
sys.path

and poking around directories a bit.

Third, it seems like Python likes calling the processing libraries like this better:

from processing.core.Processing import Processing
Processing.initialize()

In the end, my successful working script that was modified off of another answer looks like this:

#!/usr/bin/python
# Prepare the environment
import sys
# Prepare processing framework 
sys.path.append('/usr/share/qgis/python/plugins')
from qgis.utils import *
uninstallErrorHook() 
from qgis.core import QgsApplication
from PyQt4.QtGui import QApplication
app = QApplication([], True)

QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()
from processing.core.Processing import Processing
Processing.initialize()


print Processing.algs

# Exit applications
QgsApplication.exitQgis()
QApplication.exit()

Still can't get processing.alglist() to work but the Processing class seems equally promising and seems like it does the same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.