2

The script below works perfectly fine in my computer. But when running the script on another computer, I receive an error:

Error: Algorithm not found

The computer has similar specifications to my own (Windows 7, 64-bit, no admin restrictions). I did the following steps for the computer to match the scripts requirements:

  • Installed OSGeo4W 64-bit, QGIS 2.6.1
  • Copied the processing folder from C:\OSGeo4W64\apps\qgis\python\plugins\processing\ to C:\Users\user_name\.qgis2\python\plugins\

Did I miss something obvious?

import os, sys, glob

from qgis.core import *
from qgis.gui import QgsMapCanvas
from PyQt4.QtGui import *
from os.path import expanduser
home = expanduser("~")

# Set a custom config path
QgsApplication( [], False, home + "\AppData\Local\Temp" )
QgsApplication.setPrefixPath("C:\\OSGeo4W64\\apps\\qgis", True)
QgsApplication.initQgis()
app = QApplication([])

# Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

# Prepare canvas framework from qgis_interface.py
sys.path.append( home + '/Desktop/Test//' )

# Get an iface object
canvas = QgsMapCanvas()
from qgis_interface import QgisInterface
iface = QgisInterface( canvas )

# Prepare processing framework
sys.path.append( home + '/.qgis2/python/plugins' )

# Initialize the Processing plugin passing an iface object
from processing.ProcessingPlugin import ProcessingPlugin
plugin = ProcessingPlugin(iface)
from processing.tools import *

Cellsize = 1000
layerPath = path_dir + "Layer.shp"
extent = QgsVectorLayer( layerPath, '', 'ogr' ).extent()
centerx = (extent.xMinimum() + extent.xMaximum()) / 2
centery = (extent.yMinimum() + extent.yMaximum()) / 2
width = extent.xMaximum() - extent.xMinimum()
height = extent.yMaximum() - extent.yMinimum()

def run():

    outputs_1=general.runalg("qgis:creategrid", Cellsize, Cellsize, width, height, centerx, centery, 1, 'EPSG:7405', path_res  + "/"+ fname)

run()
QgsApplication.exitQgis()
app.exit()

It appears that the script cannot find any processing algorithm (from qgis, GRASS or SAGA). It also does not recognise PyQt4.Core whereas it does on my machine. Could this be relevant?

1

The problem with your procedure is that you probably used two different Processing versions, v.2.2.0-2 on your computer and v.2.6 on the other computer. Since v.2.6, Processing comes as a core QGIS plugin. It is installed in /usr/share/qgis/python/plugins/ (C:\OSGeo4W64\apps\qgis\python\plugins on Windows) and not in ~/.qgis2/python/plugins/ (C:\Users\user_name\.qgis2\python\plugins\ on Windows), like the one you install from the QGIS plugin repository.

You could think of removing your Processing v.2.2.0-2 and stick to v.2.6, but there is a caveat. Processing v.2.6 has dropped the use of custom iface objects (I've already asked Victor Olaya about it) that you were using for v.2.2.0-2. This implies that you will not be able to run algorithms that make use of iface, such as the QGIS Field Calculator algorithm, from standalone scripts.

If you decide to switch to Processing v.2.6, you must adjust the path to the proper Processing folder in your script. The following script is based on yours. I've changed paths to run it on GNU/Linux, so please adjust those paths to fit your environment. As you notice, I also don't make use of an iface object in the script, as it is no longer supported by Processing v.2.6. The script should run on both computers if they have the Processing v.2.6 installed in the same location.

import os, sys, glob

from qgis.core import *
from qgis.gui import QgsMapCanvas
from PyQt4.QtGui import *
from os.path import expanduser
home = expanduser("~")

# Set a custom config path
QgsApplication( [], False, "/tmp/" )
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()
app = QApplication([])

# Folder path of the Results for shapefiles
path_dir = home + "/Test/"
path_res = path_dir + "/Results/"

# Prepare processing framework 
sys.path.append( "/usr/share/qgis/python/plugins" )
from processing.core.Processing import Processing
Processing.initialize()

from processing.tools import *

Cellsize = 1000
layerPath = path_dir + "Shapefiles/shp1.shp"
extent = QgsVectorLayer( layerPath, '', 'ogr' ).extent()
centerx = (extent.xMinimum() + extent.xMaximum()) / 2
centery = (extent.yMinimum() + extent.yMaximum()) / 2
width = extent.xMaximum() - extent.xMinimum()
height = extent.yMaximum() - extent.yMinimum()

def run():
    outputs_1=general.runalg("qgis:creategrid", 1, width, height, Cellsize, Cellsize, centerx, centery, 'EPSG:7405', path_res + "res.shp")

run()
QgsApplication.exitQgis()
app.exit()

The order of arguments for the qgis:creategrid algorithm seems to have changed in Processing v.2.6, be careful with it. Let me know if you face any trouble with the script.

  • 1
    Tested this and works successfully, thank you very much. I will stick with v2.2.0-2 at the moment as I need the field calculator algorithm but hopefully there's a way to use v2.6 for this. – Joseph Feb 2 '15 at 10:16
1

The initial path setting might be off.

QgsApplication.setPrefixPath("C:\\OSGeo4W64\\apps\\qgis", True)

Maybe try setting this path to a variable, then use the variable in the set path

setPath = 'C:\OSGeo4W64\apps\qgis'
QgsApplication.setPrefixPath(setPath, True)

OR maybe this?

QgsApplication.setPrefixPath(r'C:\OSGeo4W64\apps\qgis', True)

I hope this helps!

  • Thanks Bennet but unfortunately, I still receive the same error. I also changed all terms including home to the specific directories but no luck. – Joseph Jan 30 '15 at 14:12
  • That's a bummer! oh well worth a shot eh? – Bennett Hawley Jan 30 '15 at 14:27
  • Hehe ofcourse :) – Joseph Jan 30 '15 at 14:29
1

I managed to get the script to run by copying the processing folder from my computer and placing it in the .qgis2 of the other computer. It seems there's something missing in the other computer although I'm not sure what.

I did notice that when I installed QGIS via the OSGeo4W installer, there were no plugin folders in \.qgis2\python\plugins\. They only appear when they are downloaded and installed via QGIS..

I will try to find what was originally missing in the other computer and report back. My thanks to @BennettHawley and @WhiteboxDev for your help!

  • Why did you update the processing plugin? Processing is now a core plugin (ver 2.6), while the one in the repository is experimental, and only version 2.2.0-2. I get a warning that the one installed is newer than the repository offers. – AndreJ Jan 30 '15 at 15:19
  • Hi @AndreJ, unfortunately I cannot remember what steps I made exactly (I have several versions of QGIS and when I uninstalled one, I was asked in another to update) but I will edit that out. Could I ask, how does the processing folder appear in the \.qgis2\python\plugins directory? – Joseph Jan 30 '15 at 15:33
  • 1
    You might have used the plugin installer and got an older version, or it is from QGIS 2.0 before it was moved into core. It might be safe to remove that, and use the core processing exclusively. – AndreJ Jan 30 '15 at 16:02
  • Thanks @AndreJ, could very well be I received an older version. Using my computer, I linked the script to the core processing plugin (C:\OSGeo4W64\apps\qgis\python\plugins) and received the same error as above. Linking it to processing plugin in \.qgis2\python\plugins works perfect. There are additional files in .qgis2 but how they got there, I have no idea. I never downloaded anything from repository, only through the Manage & Install Plugins via QGIS. – Joseph Jan 30 '15 at 16:18
  • 1
    Sorry @Joseph, it was me who told you to install v.2.2.0-2 for running your scripts. I actually did not realize I was downgrading Processing when I installed it from the repository and find that a bit confusing for users (even if there is a warning, which I didn't notice). I'll try to find how to work with v.2.6. – Germán Carrillo Jan 30 '15 at 16:38

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.