pythonmacosqgispyqgis

Segmentation Fault 11 when using initQgis() - MacOS


I am using QGIS.3.16 on macOS Catalina v.10.15.7

I have a problem when i run the following code

export PATH=/Applications/QGIS.app/Contents/MacOS/bin
export PYTHONPATH=/Applications/QGIS.app/Contents/Resources/python
export LD_LIBRARY_PATH=/Applications/QGIS.app/lib
export DYLD_LIBRARY_PATH=/Applications/QGIS.app/lib
export QT_QPA_PLATFORM_PLUGIN_PATH=/Applications/QGIS.app/Contents/PlugIns/platforms

python3

## IMPORT QGIS
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from qgis.core import *
from qgis.gui import *
from qgis.core import QgsApplication

# SET UP ENV
qgishome = "/Applications/QGIS.app/Contents/MacOS"
QgsApplication.setPrefixPath(qgishome, True)
app = QgsApplication([],False)
app.initQgis()

when i run this in terminal i get following error code

Segmentation fault: 11

I understand that 11 means row 11, and that is the app.initQgis() so something is not working there.

I ran the same code in Jupyter Notebook, but the error did not appear then. I tried restarting the computer to "clean" the memory but still same error.

Furthermore, i used the same code when using QGIS.3.14 and that worked fine, but now I get an error every time.

Any thoughts on what could be wrong?

***************************** EDIT ***********************************

Follow up on this. I installed QGIS3.10 LTR and tried the same and then the error did not appear. however i had to add GDAL_PATH in order to open shapefiles.

# Set Paths
export PATH=/Applications/QGIS3.10.app/Contents/MacOS/bin
export PYTHONPATH=/Applications/QGIS3.10.app/Contents/Resources/python
export LD_LIBRARY_PATH=/Applications/QGIS3.10.app/lib
export DYLD_LIBRARY_PATH=/Applications/QGIS3.10.app/lib
export QT_QPA_PLATFORM_PLUGIN_PATH=/Applications/QGIS3.10.app/Contents/PlugIns/platforms/
export GDAL_DATA=/Applications/QGIS3.10.app/Contents/Resources/gdal

python3

## IMPORT QGIS
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from qgis.core import *
from qgis.gui import *
from qgis.core import QgsApplication

# SET UP ENV
qgishome = "/Applications/QGIS3.10.app/Contents/MacOS"
QgsApplication.setPrefixPath(qgishome, True)
app = QgsApplication([],False)
app.initQgis()

Solution

  • The script is picking the system sqlite which is not compatible with the bundled QGIS's sqlite library. Force the script to preload the QGIS's version by:

    export DYLD_INSERT_LIBRARIES=/Applications/QGIS$QGIS_VERSION.app/Contents/MacOS/lib/libsqlite3.dylib