I import a .py file like so:
import sys
sys.path.append('/foo/bar/boo/far/')
# import foobar.py from target folder
import foobar
This line (simplified for sharing) works when the code is typed directly in the action editor window instead of importing the external script:
ft = lyr.getFeature([% $id %])
But when I use the same line in the external script, a syntax error is being thrown:
ft = lyr.getFeature([% $id %])
^
SyntaxError: invalid syntax
I continued with direct coding inside the action editor, which is possible of course, but working with an external script would significantly save time!
Any thoughts?
1 Answer 1
[% $id %]
is special QGIS syntax for "before sending this text as code to the Python interpreter, insert the value of the QGIS expression $id
" here.
A Python interpreter does not know about QGIS' special [%%]
syntax and thus will stumble.
You will need to provide the ID value in a normal Python way there.
-
Thank you! That definitely helps for finding a solution. I'm still (almost) a PyQGIS newbie. Is
iface
the way to go here, usingiface.activeLayer()
to access the action's layer? And is there aniface
way to access the clicked feature? I tried to find "a normal Python way" but did not find it so far.winnewoerp– winnewoerp2023年04月20日 04:51:02 +00:00Commented Apr 20, 2023 at 4:51 -
2Please create new questions (one question per question) for your other enquiries, as they have no relation to your original post here. It helps to keep Questions and Answers concise and to the point, otherwise things will become jumbled.Matt– Matt2023年04月20日 07:53:51 +00:00Commented Apr 20, 2023 at 7:53
-
Thanks, Matt. That's what I did now: https://gis.stackexchange.com/questions/458133/identify-clicked-feature-in-external-python-script-for-qgis-layer-actionwinnewoerp– winnewoerp2023年04月20日 23:13:07 +00:00Commented Apr 20, 2023 at 23:13