1

I'm trying to find a solution to launch a plugin from outside the QGIS console

I've succeeded to call the plugin from the python console in the QGIS software but I'm now trying to do it outside of QGIS when it's already opened

for QGIS plugin in python console the following works to launch the plugin:

import sys
sys.path.append(r"path/to/plugin")
from plugin_name_folder import plugin_name
execute = plugin_name.plugin_class(iface)
execute.initGui()
execute.run()

in order to launch the plugin, here's the beginning of the code pre-made by the plugin builder :

from PyQt5.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QAction

from .resources import *
from .zoom_snap_dialog import zoom_snapDialog
import os.path


class zoom_snap:
    def __init__(self, iface):
        self.iface = iface
        self.plugin_dir = os.path.dirname(__file__)
        locale = QSettings().value('locale/userLocale')[0:2]
        locale_path = os.path.join(
            self.plugin_dir,
            'i18n',
            'zoom_snap_{}.qm'.format(locale))

        if os.path.exists(locale_path):
            self.translator = QTranslator()
            self.translator.load(locale_path)

            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)

        self.actions = []
        self.menu = self.tr(u'&(name)')

        # Check if plugin was started the first time in current QGIS session
        # Must be set in initGui() to survive plugin reloads
        self.first_start = None

When I try to launch my plugin with plugin_name.plugin_class(iface) I'm hitting the following error :

"locale_path=os.path.join("
TypeError: 'NoneType' object is not subscriptable

I've seen that another line before generates the same TypeError when launched separately in window command line :

QSettings().value('locale/userLocale')[0:2]
TypeError: 'NoneType' object is not subscriptable

My questions will be :

1- Where is QSettings().value([path])[0:2] extracting values from ? I can't see where locale/userLocale folder is and how is it taking its values

2- I've seen a lot of posts about the 'NoneType' TypeError posts but here I don't know if anybody had any clue about solving this type of error

1

For question 1:

The settings (QSettings) are read from QGIS itself. You can access the value in Preference > Options > Advanced. Click on the button to say that you will be careful and you access all the values. There is a locale/userLocale settings that define the language to use in QGIS.

For question 2:

The NoneType object is not subscritable error just mean that your are trying to subscript (with the slice [0:2]) a NoneType object which is not possible.

It seems that launching your plugin from console, doesn't allow it to access QSettings the proper way... (you get None when trying to access values). To make it work you can (as a workaround) delete the call to QSettings in your code. Note that if you do that you need to implement another way to load i18n if you want to use i18n in your plugin.

  • 1
    As you are building a plugin for QGIS this is not a standalone program. If you really need to write a standalone program with pyqgis have a look at geospatialdesktop.com/2009/02/… You will need to adapt the code to pyqt5 and pyqgis3 – YoLecomte Feb 10 at 19:43
  • Thank you for you answer, I've removed the block containing the os.path file but now I'm struggling with missing DLL's files for PyQt5.QtCore ... Seems like it makes it really hard to get a standalone program I've got : ImportError : DLL load failed : the specified module could not be found I've tried to change the PATH for dll folders of QGIS but it doesn't seem to do any better The thing is that with the QGIS Python console I can do all the steps without a problem, but when it comes about a standalone script, it's messing up and I don't understant C++ enough to get deep in QGIS docs – Brocolus Feb 10 at 19:46
  • Sorry I could'nt edit so I've made a new comment, thank you for the link! – Brocolus Feb 10 at 19:47
  • Glad to help! Your problem is related to settings a proper environnent to run your code. So if you face any problem on this topic please ask a new question that focus on your specific problem. – YoLecomte Feb 10 at 20:54

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.