2

I configured SPYDER to work with QGIS by setting start file and setting additionally some system paths

@echo off
SET OSGEO4W_ROOT=C:\PROGRA~1\QGIS2~1.18
SET QGISNAME=qgis
SET GRASSNAME=grass\grass-7.2.1
SET QGIS=%OSGEO4W_ROOT%\apps\%QGISNAME%
SET QGIS_PREFIX_PATH=%QGIS%
SET GRASS=%OSGEO4W_ROOT%\apps\%GRASSNAME%
SET SPYDER="C:\\PROGRA~1\QGIS 2.18\apps\Python27\Scripts\spyder3.exe"

CALL "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%GRASS%"\etc\env.bat
path %PATH%;%QGIS%\bin
path %PATH%;%GRASS%\lib


set PYTHONPATH=%QGIS%\python;%PYTHONPATH%
set PYTHONPATH=%OSGEO4W_ROOT%\apps\Python27\Lib\site-packages;%PYTHONPATH%
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python\plugins\processing;%PYTHONPATH%
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python\plugins;%PYTHONPATH%


start "SPYDER aware of QGIS" /B %SPYDER% %*

import sys
print sys.path

sys.path.append('C:/PROGRA~1/QGIS2~1.18/apps/qgis/./python')

sys.path.append('C:/PROGRA~1/QGIS2~1.18/apps/qgis/./python/plugins')

sys.path.append('C:/PROGRA~1/QGIS2~1.18/apps/qgis/./python/plugins\\processing')

after those settings all imports are OK. However when i try to load a layer it fails, but the same code works in QGIS Desktop python environment. My goal is to use SPYDER (or another IDE e.g. Eclipse) to make life easier when debugging/designing QGIS scripts. Is it possible to integrate IDE with QGIS to acheve this ??

here is my code

from osgeo import ogr
from PyQt4.QtCore import QVariant
from qgis.core import QGis, QgsFeature, QgsField, QgsFields, QgsVectorLayer, QgsMapLayerRegistry
from processing.tools.vector import VectorWriter
import csv

from PyQt4.QtGui import *


wb = QgsVectorLayer('C:\\Users\\krzysztof.fajst\\Documents\\PYTHON\\GIS_LV networks\\os with roads feeders and substations.shp', 'os with roads feeders and substations', 'ogr')
layer = QgsMapLayerRegistry.instance().addMapLayer(wb)

also simple

layer = qgis.utils.iface.activeLayer()

fails saying qgis name not defined even if I imported iface with

from qgis.utils import iface

Krzysztof

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.