I made a python script in QGIS-3 that runs pretty well as a layer action. It reads an excel file, loads its content in Postgresql, triggers pg functions, then refreshes the canvas to display the result in an already loaded postgis layer.
Since it does not have any relationships with a specific layer, I thought it would rather be stored in the Processing toolbox, but when I run it from there I get an error saying :

NameError: name 'qgis' is not defined

I searched and found that it was because of a missing import, but my imports seem already satisfying:

import time import os, sys, psycopg2, csv, xlrd
from PyQt5 import Qt from PyQt5.QtCore import QSettings from qgis.gui import QgsMessageBar from PyQt5.QtWidgets import QFileDialog from PyQt5.QtWidgets import QApplication from qgis.core import * from qgis.utils import iface

The lines that cause the problem are the following:

if xmin is not None:

canvas = qgis.utils.iface.mapCanvas()
bb = QgsRectangle(xmin, ymin, xmax, ymax)

I have tried with

from qgis.utils import *

but that made no difference.

I'm really not comfortable with python, could someone maybe help ?

  • What is the qgis object in your action code? Did you import qgis.utils in it? Jul 28 at 13:53

1 Answer 1


Since you use from qgis.utils import * then you can use the iface object directly, not via qgis.utils.iface.

  • 1
    It was that simple indeed ! Thank you very much @bugmenot123 .
    – dd_a
    Jul 31 at 9:27

Your Answer

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

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