layer failed to load, PyQGIS in MacOS Mojave

by nospec   Last Updated October 10, 2019 17:22 PM

The following is the code that I wrote with the help of PyQgis Developer Cookbook.

from qgis.core import *
from qgis.gui import *
import os

Reference to QgsApplication

qgs = QgsApplication([], False)

path to QGIS install Location

QgsApplication.setPrefixPath("/Applications/", True)

Load providers


print (QgsApplication.showSettings())

loading projects

load current project

project = QgsProject.instance()

read project from file,'Documents/GIS/Kandahar/Project/Kandahar.qgz'))

fileName() returns the path of the project

print (project.fileName())

loading Vectors

vpath = os.path.join(project.homePath(), "Documents","GIS", "Test","farmland.shp")
vlayer = QgsVectorLayer(vpath, "Farmland", "ogr")
if vlayer.isValid():
    print("Layer loaded")
    print("Layer failed to load")`

I have also edited my .bash_profile to include system paths as export PATH=/Library/Frameworks/GDAL.framework/Programs:$PATH export LD_LIBRARY_PATH=/Applications/$LD_LIBRARY_PATH export LD_LIBRARY_PATH=/Applications/$LD_LIBRARY_PATH export PYTHONPATH=/Applications/$PYTHONPATH export PYTHONPATH=~/.qgis2/python/plugins/opengeo/ext-libs:$PYTHONPATH export PATH=/Applications/$PATH export QT_QPA_PLATFORM_PLUGIN_PATH=/Applications/ export QGIS_PREFIX_PATH=${QGIS_BUNDLE}/MacOS export PATH=${QGIS_PREFIX_PATH}/bin:$PATH

my output is the following

Application state:
QGIS_PREFIX_PATH env var:       /MacOS
Prefix:     /MacOS
Plugin Path:        /MacOS/../PlugIns/qgis
Package Data Path:  /MacOS/../Resources
Active Theme Name:  
Active Theme Path:  /MacOS/../Resources/resources/themes//icons/
Default Theme Path: :/images/themes/default/
SVG Search Paths:   /MacOS/../Resources/svg/
    /Users/joelrhine/Library/Application Support/profiles/default/svg/
User DB Path:   /MacOS/../Resources/resources/qgis.db
Auth DB Path:   /Users/joelrhine/Library/Application     Support/profiles/default/qgis-auth.db

Layer failed to load
[Finished in 0.493s]`

Related Questions

Updated November 15, 2018 14:22 PM

Updated April 15, 2019 08:22 AM

Updated November 15, 2018 13:22 PM

Updated June 13, 2017 01:22 AM