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

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Nov 2, 2020 at 13:28
6
  • On your second image I do not see break command. Can you try using it? Commented Nov 2, 2020 at 13:31
  • the same, didnt get any output.. Commented Nov 2, 2020 at 13:33
  • What if you try print(lyr) before for-loop. What does it give you? Commented Nov 2, 2020 at 13:36
  • >>> print(lyr) <QgsMapLayer: 'PRG_PunktyAdresowe_POLSKA' (ogr)> Commented Nov 2, 2020 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 Commented Nov 2, 2020 at 13:42

1 Answer 1

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.

answered Nov 2, 2020 at 13:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.