3

I am trying to generate a standalone exe using the pyqgis and related libraries.

I have borrowed from the app in the book 'Building Map Applications with QGIS', by Erik Westra and published by PACKT.

The code has been adapted in relation to this answer: Working Windows Standalone .EXE From QGIS Python Script Using PyInstaller?

Also the from xxxxx import * lines have been replaced and code updated accordingly. I have read from multiple sources that this can bother pyinstaller.

I have put all the code and associated bat files into a repository if anyone wants to clone it to try it out: https://github.com/benboughton1/offlinemap

run.bat which is just running the app using standard python environment works fine.

pyinstaller runs OK too, but when I try to run the app I get the error:

Qt: Untested Windows version 10.0 detected!
LOADER: Running externalApp.py
Traceback (most recent call last):
  File "externalApp.py", line 4, in <module>
  File "c:\osgeo4~1\apps\python27\lib\site-packages\PyInstaller-3.2.1-py2.7.egg\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
    exec(bytecode, module.__dict__)
  File "qgis\__init__.py", line 26, in <module>
  File "c:\osgeo4~1\apps\python27\lib\site-packages\PyInstaller-3.2.1-py2.7.egg\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
    exec(bytecode, module.__dict__)
  File "qgis\PyQt\QtCore.py", line 28, in <module>
ValueError: API 'QDate' has already been set to version 1

Also the warning txt file has several missing modules:

missing module named PySide - imported by C:\OSGEO4~1\apps\Python27\lib\site-packages\pyinstaller-3.2.1-py2.7.egg\PyInstaller\loader\rthooks\pyi_rth_qt4plugins.py
missing module named org - imported by copy
missing module named qgis.core.QgsFeature - imported by qgis.core, qgis
missing module named qgis.core.QgsGeometry - imported by qgis.core, qgis
missing module named fcntl - imported by tempfile, subprocess
missing module named readline - imported by cmd, pdb
missing module named pwd - imported by posixpath
missing module named 'org.python' - imported by pickle
missing module named riscosenviron - imported by os
missing module named riscospath - imported by os
missing module named riscos - imported by os
missing module named ce - imported by os
missing module named _emx_link - imported by os
missing module named os2 - imported by os
missing module named posix - imported by os
missing module named resource - imported by posix
0

Answer lies in order in which pyqt4 and pyqgis libraries are imported.

Qgis libraries need to be imported first, then PyQt4.

It seems that when written in this order pyinstaller still will import PyQt4 first in the compiled application which causes the error.

To get around this, I created a hook.py file that is included in the pyinstaller process which runs before the app.py file. This forces the qgis libraries to be imported first and solves the error.

The git repo has been updated according to the answer.

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.