2

When I run my code in QGIS Python console I get output, but when I run my code from normal console I didn't get any output.

Why does it work only in QGIS Python console?

import sys, os

sys.path.append(r'C:/PROGRA~1/QGIS3~1.16/apps/qgis/python/plugins')
os.environ['PROJ_LIB'] = 'C:\\Program Files\\QGIS 3.16\\share\\proj'

from qgis.core import *
from qgis.utils import *

qgispath = r'C:/PROGRA~1/QGIS3~1.16/apps/qgis'
 
QgsApplication.setPrefixPath(qgispath, True)
qgs = QgsApplication([], False)
qgs.initQgis()

path = r'C:/Users/ja/Inne/Desktop/POLSKA/PRG_PunktyAdresowe_POLSKA.shp' 
lyr = QgsVectorLayer (path, 'PRG_PunktyAdresowe_POLSKA', 'ogr')

for f in lyr.getFeatures():
    print(f.attributes())
    break

enter image description here

enter image description here

6
  • On your second image I do not see break command. Can you try using it? – Taras Nov 2 '20 at 13:31
  • the same, didnt get any output.. – datasciencebegginer Nov 2 '20 at 13:33
  • What if you try print(lyr) before for-loop. What does it give you? – Taras Nov 2 '20 at 13:36
  • >>> print(lyr) <QgsMapLayer: 'PRG_PunktyAdresowe_POLSKA' (ogr)> – datasciencebegginer Nov 2 '20 at 13:39
  • Try print(lyr.featureCount()) before forloop. I think there are no entities or it can't read and iterate on your layer – Vincent Bré Nov 2 '20 at 13:42
4

In your last screenshot where we see the Python interpreter in an interactive session you have not actually started execution of the loop yet. Hit the Enter key one more time.

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.