QGIS 2.14 on Windows 7. I'm trying to call a method in a python script from the QGIS python console. I've put the directory in which the scripts resides in the PYTHONPATH in both the windows system and user environment variable.
The python script (first_script.py) has a method that looks like this:
from PyQt4.QtGui import * from PyQt4.QtCore import * from qgis.core import * from qgis.utils import iface def load_layer(): wb = QgsVectorLayer('C:\Data\scripts\pyqbook\data\world_borders.shp', 'world_borders', 'ogr') QgsMapLayerRegistry.instance().addMapLayer(wb)
I call the script like this:
>>>import first_script >>>first_script.load_layer()
and I get QGIS error: "Data source is invalid". However, when I then manually enter the two lines of the method, it works fine and loads the layer into the canvas correctly:
>>>wb = QgsVectorLayer('C:\Data\scripts\pyqbook\data\world_borders.shp', 'world_borders', 'ogr') >>>QgsMapLayerRegistry.instance().addMapLayer(wb)
Why can't I call the method from the QGIS python console directly without getting the "Data source is invalid" error? The data source is valid because I can call the individual lines of the method and get the correct result.