2

We have developed a few PyQGIS standalone applications that bring in PyQt5.Qtcore, PyQt5.QtGui, and qgis.core to display map and do custom logics etc. The applications started with QGIS python 2.7 all the way to QGIS python 3.6, over time we found that it is getting increasingly difficult to get the libraries setup correctly and packaging the application.

I would like to emphasize that these are applications that are completely standalone without needing to open QGIS program, they are NOT run from inside QGIS's python console.

Now, it is QGIS 3.16 LTR with Python 3.8, after upgrade QGIS, our apps stopped working, this happens pretty much every time there is a new version of QGIS. The QGIS installation structure changed a lot with this new version (just like previous version changes), to name a few things for example, apps/qt5/include and apps/qt5/doc directories are no longer there, and the following imports are no longer working:

from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication, QMainWindow, QUndoCommand, QUndoStack, QFileDialog, QVBoxLayout, QAction
from PyQt5.QtWidgets import QAbstractItemView, QMessageBox, QMenu, QInputDialog, QDockWidget

Does QGIS still intend to support standalone application development or should we abandon these apps and build plugins instead? I could NOT find an up-to-date programmer's guide for developing standalone applications, most of the PDF instruction files on the internet are for much older versions of QGIS.

A bit more information about our application setup. We develop using PyCharm on Windows 10 OS, and we install QGIS using its installer, not from package, for example, for this latest version, we used QGIS-OSGeo4W-3.16.11-1.msi and install into C:\OSGeo4W64 directory, then in PyCharm, we set python interpreter to C:\OSGeo4W64\apps\Python39\python.exe (the last version was pointing to Python36\python.exe), this will pull in all packages that come with the QGIS software and this has been the way we've been developing the applications. This is because from past experience (since QGIS python 2.7), we have to use all python libraries/packages that come with QGIS inside its own installation directories, because it appears that the qgis.core is built with its own versions of every package that is included in QGIS software itself. We tried in the past to point to outside PyQt libraries and couldn't get qgis.core to work in standalone application until we reference everything back into QGIS software itself.

We look forward to hearing a more definitive answer and any migration tips/guide.

15
  • 1
    In my opinion, this question is more suitable for the QGIS-dev mailing list, where most of devs and even members of the PSC will read your question. lists.osgeo.org/mailman/listinfo/qgis-developer Sep 25 '21 at 1:22
  • 2
    On QGIS's main page under user community, gis.stackexchange.com is prominently listed as a place to ask any QGIS questions: qgis.org/en/site/forusers/support.html StackExchange¶ On gis.stackexchange.com you can ask QGIS questions also. If you use the tag qgis you’ll see all QGIS related questions and answers: gis.stackexchange.com/?tags=qgis; also, this question doesn't relate to any of the things you listed: licensing, pricing, release dates, submission of bug reports and enhancement requests, it strictly is related to the issue with QGIS library per app development Sep 25 '21 at 4:44
  • 2
    Your question relates to QGIS product planning so it pertains to the business of the QGIS development team, and can only be answered definitively by them.
    – PolyGeo
    Sep 25 '21 at 6:39
  • 2
    This problem is at your end, your packaging is broken. Sep 25 '21 at 8:28
  • 2
    Try to change all PyQt5 in import section to qgis.PyQt. For example: from PyQt5 import QtCore, QtGui into from qgis.PyQt import QtCore, QtGui. Sep 25 '21 at 18:42
4

Standalone PyQGIS application are still supported. I'm still developing apps.

I had the same problem at the beginning of developing standalone PyQGIS3 apps. Changing all PyQt5 in import section to qgis.PyQt solved the PyQt issue. For example:

from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *

to

from qgis.PyQt import QtCore, QtGui
from qgis.PyQt.QtGui import *
7
  • Actually, earlier versions of QGIS in python 3 (e.g. the 3.6 version/Noosa) actually used the PyQt5 namespace. What's more, when packaging our app, we discovered that there needs to be a standalone 'src' folder that contains some of the key libraries that needs to be distributed with the packaged program. I have not tried with this new version yet. But I am frustrated in that it seems that every version of QGIS, these namespaces change without warning/documentation, which makes standalone application development increasingly dicey. Sep 26 '21 at 2:15
  • Our app also has an import: import sip, it is not working anymore, I clearly see 'sip' package in the QGIS site-packages, but it is not recognizing it with this new version, perhaps it is referenced via different namespace now? thx. Sep 26 '21 at 2:18
  • Where have all of the QtWidgets gone? i.e. QApplication, QMainWindow, QMenu etc etc are all missing. I tried multiple namespaces paths, none leads to any of the QtWidgets! When I navigate to QtWidget declaration in PyCharm, it still says import PyQt5 ! Sep 26 '21 at 3:33
  • 1
    import sip to from qgis.PyQt import sip Look at github.com/qgis/QGIS/wiki/… (for plugin but could help to upgrade). See also qgis.org/api/api_break.html#qgis_api_break_3_0
    – ThomasG77
    Sep 26 '21 at 17:24
  • @ThomasG77 this works! so two down, more to go in our upgrade effort. I think that we've already followed that qgis api break link you posted when we migrated from QGIS python 2.7 to QGIS python 3 during last round of upgrade. I hope we can spur some effort towards official cookbook development for standalone app development, well, until QGIS state otherwise. I am hoping for clarity. Sep 26 '21 at 21:20
2

This is the simple PyQGIS application from the cookbook for the latest release, with all the imports that you say are failing for you included, and /usr as the QGIS prefix path which is where QgsApplication.prefixPath() says it is. This runs perfectly from a shell:

from qgis.core import *

from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication, QMainWindow, QUndoCommand, QUndoStack, QFileDialog, QVBoxLayout, QAction
from PyQt5.QtWidgets import QAbstractItemView, QMessageBox, QMenu, QInputDialog, QDockWidget


# Supply path to qgis install location
QgsApplication.setPrefixPath("/usr", True)

# Create a reference to the QgsApplication.  Setting the
# second argument to False disables the GUI.
qgs = QgsApplication([], False)

# Load providers
qgs.initQgis()

# Write your code here to load some layers, use processing
# algorithms, etc.

# Finally, exitQgis() is called to remove the
# provider and layer registries from memory
qgs.exitQgis()

If this doesn't work for you then you must be missing either prerequisites (ie the PyQt5 stuff, which is not part of QGIS) or have a broken setup. You don't state you operating system or version of it, or any other aspects of your setup.

But I can state that PyQGIS standalone applications are not, are not planned to be, deprecated in any way, and that for me at least, those imports that don't work for you work perfectly for me.

Ubuntu 20.04 with qgis 3.20-latest etc installed via apt from qgis.org

9
  • I updated my question to give details of our setup. We started from QGIS 2.7, which has its own setup instruction which we followed to get everything working, then after QGIS moved into python 3, there is no official instruction anywhere. So we brute forced to migrate into python 3 version of QGIS using the same scheme after fixing many breaking changes in the QGIS libraries. What you showed here has been working with QGIS 3.6 version, but after we installed QGIS 3.16, it is NOT working anymore. Just see Kadir's reply, PyQt namespace is moved under qgis namespace with this new version! Sep 26 '21 at 16:21
  • Why is Kadir's answer removed? Sep 26 '21 at 16:36
  • So are you saying PyQt5 is not part of QGIS installation? then I don't know how that is possible as we have been developing using QGIS 3.6 and we used QGIS's own PyQt5 libraries and we do not have any other pyqt libraries on our development machine. The bottom line is, this code you posted worked fine when we were using QGIS 3.6 and we have not changed anything else except installing this new version of QGIS 3.16 and it is failing. So if it is due to missing prerequisites and that we've been using everything from QGIS installation, then this only points to new QGIS missing some prerequisites. Sep 26 '21 at 17:19
  • 1
    QGIS is a piece of software that comes with no warranty. QGIS is not an entity that can "support" anything. If you want support for anything in QGIS you can pay for it and get support for it and get a warranty with that. If you want to improve standalone app support you can offer to pay for it, and someone - and because this is open source anyone - can take up your offer. But for most of us it seems the functionality is acceptable.
    – Spacedman
    Sep 26 '21 at 21:17
  • 1
    This is not a place for "threads". Its a Q+A site. You really should take this to the qgis-dev mailing list.
    – Spacedman
    Sep 27 '21 at 9:39

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.