I'm building a QGIS 3.0 plugin and trying to organise my code into several files. I have three files – a main python script and two other scripts stored in a subfolder. Excerpts provided below:
\myplugin.py:
from .mod import pmp as pmp
from .mod import helpers as h
class MyPlugin:
def __init__(self, iface):
self.iface = iface
[...]
h.msgInfo(self,"Plugin initialised!")
pmp.test(self)
\mod\helpers.py:
import qgis.core as qgisCore
from qgis.core import Qgis, QgsApplication, QgsMessageLog
from qgis.gui import QgsMessageBar
def msgInfo(self, message, **kwargs):
title = kwargs.get('t','')
dur = kwargs.get('d',10)
self.iface.messageBar().pushMessage(title, message, level=qgisCore.Qgis.Info, duration=dur)
\mod\pmp.py:
from . import helpers as h
def test(self):
h.msgInfo(self,"PMP module successfully imported!")
Importing "helpers" is working perfectly for myplugin.py, as the "Plugin initialised!" message is showing as expected. Similarly, importing "pmp" seems to be working for myplugin.py. But when myplugin.py gets to this line:
pmp.test(self)
I get this error:
TypeError: test() takes 0 positional arguments but 1 was given
I've tried "self.pmp.test()" instead, but then of course I get "AttributeError: 'myplugin' object has no attribute 'pmp'".
If I use just "pmp.test()", then pmp.py gives me "NameError: name 'self' is not defined".
Best Answer
self
is an internal variable that is a reference to the instance of your plugin class.Don't pass self (itself :) to
h.msgInfo
, pass theiface
object.\myplugin.py:
\mod\helpers.py:
\mod\pmp.py: