5

I am developing a standalone app in Python, using QGIS libraries including processing algorithms. I am developing in Linux environment and I put the libraries in a folder. Also I put the processing folder from QGIS in that same folder. I run the app but when I import the processing folder, it gives me nothing (not even error messages).

I import like that:

import processing.core.Processing

Can you help me?

CODE:

import sys
import qgis.core
import PyQt4.QtCore
import PyQt4.QtGui
app = qgis.core.QgsApplication([],True)
qgis.core.QgsApplication.setPrefixPath("/usr", True)
qgis.core.QgsApplication.initQgis()

(from here the program doesn't work)

from processing.core.Processing import Processing
inputLayer = 'c:....'
inputMask = 'c:....'

extent = ....
cellsize = ....
outPath = 'c:.....'

Processing.Initialize()
Processing.runalgorith(.....)

I downloaded the folder with the processing version 2.2.0-2 and I copied to my script folder. I run and it gives me this error:

Traceback (most recent call last):
  File "teste.py", line 12, in <module>
    from processing.core.Processing import Processing
  File "/home/p/Programas/processing/__init__.py", line 28, in <module>
    from processing.tools.dataobjects import *
  File "/home/p/Programas/processing/tools/dataobjects.py", line 33, in <module>
    from processing.core.ProcessingConfig import ProcessingConfig
  File "/home/p/Programas/processing/core/ProcessingConfig.py", line 30, in <module>
    from processing.tools.system import *
  File "/home/p/Programas/processing/core/processing.py", line 33, in <module>
    from processing import interface
ImportError: cannot import name interface

I got it!! What is missing in my case was to define the PYTHONPATH to the folder with the processing. Now I have the correct running of the code.

  • I asked several questions regarding standalone python apps and @gcarrillo has helped me greatly (he also uses linux) so perhaps you can find some help with scripting there. Otherwise, maybe try from processing.core.Processing import Processing? – Joseph Jan 26 '15 at 12:27
  • I see very questions and answers and I try everything but I didn't have any result. And I try your suggestion also. Basically, when I run with that import, it stops reading the script in that part. – Bárbara Duarte Jan 26 '15 at 12:32
  • Could you post your script by editing your question please? Hopefully others will recognise the problem and provide a solution. – Joseph Jan 26 '15 at 12:36
  • Sorry but I am using another computer to develop the code. But basically, I import the qgis.core, PyQt4.QtCore and PyQt4.QtGui, define, the input and outpu directory, the extent and cellsize and then I want to use processing algorithms. In here I can´t do nothing. – Bárbara Duarte Jan 26 '15 at 12:40
  • 1
    Not enough information to help you with that. You should be more specific, even more if you changed your libraries location (I don't know why you do that). You should, at least, include the script you are trying to run, as @Joseph said. – Germán Carrillo Jan 26 '15 at 15:46
5

Right, I've tested your script and, as you say it doesn't throw anything (not even errors) when I run it.

Try instead this script, it works for me on Linux, emulating your scenario (moving processing folder to my script folder):

# Prepare the environment
import sys
from qgis.core import QgsApplication
from PyQt4.QtGui import QApplication
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

# Prepare processing framework 
sys.path.append('/home/YOUR_USER/.qgis2/python/plugins')
from processing.core.Processing import Processing
Processing.initialize()

print Processing.getAlgorithm("qgis:creategrid")

# Exit applications
QgsApplication.exitQgis()
QApplication.exit()
  • My processing is in the same folder as my script. I want to run my script appart (independent) QGIS. So I copy the processing folder to my script directory which is c:\users\username\programs. If I put this directory in sys.path.append I get the same result, nothing. Processing can run completely outside qgis? – Bárbara Duarte Jan 26 '15 at 16:29
  • Try to set your plugin path this way qgis.core.QgsApplication.setPluginPath('/path/to/where/your/processing/folder/is/') after the line where you call setPrefixPath() – Germán Carrillo Jan 26 '15 at 16:32
  • Not working. The script named teste.py is in C:\users\username\programs and the processing folder is also in that directory, so I added qgis.core.QgsApplication.setPluginPath ('C:\users\username\programs') just like you said but it doesn't work. – Bárbara Duarte Jan 26 '15 at 16:38
  • Ok, I've completely edited my answer, please read it and give it a try. If you get the help of the creategrid algorithm printed, then it worked. – Germán Carrillo Jan 26 '15 at 16:41
  • 1
    Ahem, oops! Anyway, I can confirm the script still works with the processing folder in the same directory as the script folder (and without using sys.path.append() ). One question Barbara incase I missed it, did you set your PYTHON variable paths before running the script, similar in this post? – Joseph Jan 27 '15 at 15:17

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.