6

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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Bárbara Duarte
  • 1,451
  • 4
  • 18
  • 31
  • 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
  • I write the code without the parameters. My problem is in importing processing. I try such as http://gis.stackexchange.com/questions/129915/cannot-run-standalone-qgis-script but without success. – Bárbara Duarte Jan 26 '15 at 16:18

1 Answers1

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()
underdark
  • 84,148
  • 21
  • 231
  • 413
Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
  • 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
  • No success. I try what you said and it only reads until the part #prepare processing framework". From that it stops. I don't know if that helps but I tested in command line all the imports (import qgis.core...) and in all there isn't problems. When I try from processing.core.Processing import Processing in command line I have this message: from: can't read /var/mail/processing.core.Processing – Bárbara Duarte Jan 27 '15 at 09:55
  • @BárbaraDuarte, are you running the script as Admin? This used to be a problem for me before. – Joseph Jan 27 '15 at 11:30
  • How do I see that? Sorry but its my first time with linux programming. – Bárbara Duarte Jan 27 '15 at 11:31
  • @BárbaraDuarte This is not really linux programming. Python, PyQt4 and PyQGIS are multiplatform. What @Joseph tells you is to run the script this way sudo python myscript.py from the linux console. Just being curious, do you have your processing folder in /var/mail/, and moreover, do you have a "processing" folder there (something like /var/mail/processing/) or did you change its name? – Germán Carrillo Jan 27 '15 at 14:11
  • Well I try sudo python teste.py but without success too. And I verify if I have the processing folder in /var/mail but no, I haven't. I search and I have the processing folder in my script folder (as I told you) and I try also to access to processing folder of qgis (/usr/share/qgis/python/plugins/processing) but also the program doesn't run from there. – Bárbara Duarte Jan 27 '15 at 14:28
  • If you are not able to provide us with your file structure, it's very difficult for us to guess it (since you told us you changed processing folder location). I suggest you to open a linux terminal and go to the script folder. Once you are there run python myscript.py. – Germán Carrillo Jan 27 '15 at 14:38
  • However, I would first try to run either this http://gis.stackexchange.com/questions/129513/how-to-use-qgis-explode-lines-with-python/130337#130337 or this http://gis.stackexchange.com/questions/129915/cannot-run-standalone-qgis-script/130176#130176 scripts using the processing folder that is inside QGIS plugins directory. Only after such scripts are running, I would switch to your environment (with your processing folder in another location). @Joseph do you have time to test the answer I posted? It works for me, there must be something weird in Bárbaras procedure. – Germán Carrillo Jan 27 '15 at 14:40
  • Hi @gcarrillo, ofcourse! I will test and report back. – Joseph Jan 27 '15 at 14:42
  • Apart from changing the QgsApplication.setPrefixPath for my windows directory, I also had to add sys.path.append( '/.qgis2/python/plugins' ), not sure what the linux alternative is. But with these minor changes, your script works. – Joseph Jan 27 '15 at 14:53
  • I run the program in my script directory (/home/p/Programas/) but doesn't work. Also I try add this new line:

    sys.path.append('/usr/share/qgis/python/plugins/')

    to access processing folder of QGIS but without success. I am trying hard with all the examples given in http://gis.stackexchange.com/questions/129513/how-to-use-qgis-explode-lines-with-python/130337#130337 and http://gis.stackexchange.com/questions/129915/cannot-run-standalone-qgis-script/130176#130176. Also I try with MetaSearch plugin from QGIS and the program read that but processing never works. I don't know what else to do.

    – Bárbara Duarte Jan 27 '15 at 14:55
  • I see that in very questions, the import qgis.core has to be before import PyQt4. In processing code, it is always after. This difference is relevant? – Bárbara Duarte Jan 27 '15 at 15:03
  • Thanks @Joseph! I actually meant to try the answer emulating Barbara's environment, i.e., saving the answer as a Python script and copying your processing folder to your script folder. Then, from a command line, run the script using python myscript.py. For that, you wouldn't need to use sys.path.append(). – Germán Carrillo Jan 27 '15 at 15:08
  • 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
  • I did not do that. It is necessary even running in linux console? – Bárbara Duarte Jan 27 '15 at 15:21
  • I try to run different plugins from '/usr/share/qgis/python/plugins/'and all work well. When I try with processing nothing happens. – Bárbara Duarte Jan 27 '15 at 15:25
  • No. In home folder I just have a "p" folder and a "q" folder and I don't find .qgis2. It must be better to install qgis again, no? – Bárbara Duarte Jan 27 '15 at 15:31
  • Yes, and make sure you install the latest version (2.2.0-2) of processing, which, as far as I know, is not included in QGIS out of the box. You can install it from the plugin manager. – Germán Carrillo Jan 27 '15 at 15:33
  • Ii will reinstall. But the latest version is 2.2.0-2 of Processing framework? Because I open qgis and I see in Plugin install and manager that I have version 2.6. Is it possible? – Bárbara Duarte Jan 27 '15 at 15:37
  • That's right Barbara. So my version is outdated. However, that's the version I employed running the scripts. I haven't tested processing v.2.6. – Germán Carrillo Jan 27 '15 at 15:42
  • Barbara, 2.2.0-2 is the latest version of the Processing plugin which is available in QGIS 2.6. – Joseph Jan 27 '15 at 15:44
  • The latest error you reported is a bit strange if you downloaded and extracted the plugin with no further modifications. Do you have acces to the file /home/p/Programas/processing/interface.py? – Germán Carrillo Jan 27 '15 at 17:11
  • Yes. The interface.py is in home/p/Programas/processing/ just like you said. – Bárbara Duarte Jan 27 '15 at 17:20
  • Many thanks to the precious help but I will try. I try to uninstall and reinstall qgis and see what happens. Thanks. – Bárbara Duarte Jan 27 '15 at 17:32