[Qgis-user] NameError: name 'iface' is not defined when importing script to processing toolbox

Nyall Dawson nyall.dawson at gmail.com
Mon Nov 25 15:09:26 PST 2019


On Tue, 26 Nov 2019 at 07:11, Cliff Patterson <cpatterson at psdrcs.com> wrote:
>
> When trying to add a script to the processing toolbox, I get the following error (below) even though iface is imported with "from qgis.utils import iface" in the script. The problem existed with QGIS 3.6 so I upgraded to 3.10 on Windows 10, but it persists, I am using the standalone installer. Any ideas how I can fix this error?

You should avoid using iface in processing algorithms, for a number of reasons:

1. It's not thread safe, and processing algorithms run in a background
thread by default
2. It's not available everywhere processing can be used

The addVectorLayer method exposed to iface is really just a simple
convenience wrapper, and you can achieve the same results by loading a
vector layer using QgsVectorLayer api.

Nyall


>     iface.addVectorLayer(uri, 'Asset Data', 'ogr')




> NameError: name 'iface' is not defined
>
>
> Python version: 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]
> QGIS version: 3.10.0-A Coruña A Coruña, 6ffa89eb3e
>
> Python Path:
>
> C:/PROGRA~1/QGIS3~1.10/apps/qgis/./python
> C:/Users/Cliff/AppData/Roaming/QGIS/QGIS3\profiles\default/python
> C:/Users/Cliff/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins
> C:/PROGRA~1/QGIS3~1.10/apps/qgis/./python/plugins
> C:\Program Files\QGIS 3.10\bin\python37.zip
> C:\PROGRA~1\QGIS3~1.10\apps\Python37\DLLs
> C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib
> C:\Program Files\QGIS 3.10\bin
> C:\PROGRA~1\QGIS3~1.10\apps\Python37
> C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib\site-packages
> C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib\site-packages\win32
> C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib\site-packages\win32\lib
> C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib\site-packages\Pythonwin
> C:/Users/Cliff/AppData/Roaming/QGIS/QGIS3\profiles\default/python
> C:\Users\Cliff\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins
>
>
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user


More information about the Qgis-user mailing list