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 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) canvas.setExtent(bb) canvas.refresh()
I have tried with
from qgis.utils import *
but that made no difference.
I'm really not comfortable with python, could someone maybe help ?