<div dir="ltr" gmail_original="1">For running a PyQGIS script as a standalone script, you need to set appropriate environment variables and initialize qgis in a headless mode. See the full example with setup for Windows and Mac at <a href="https://mailtrack.io/l/54a2bb23bb2e216749cf31c77063d79fd7f518f8?w=cWdpcy11c2VyQGxpc3RzLm9zZ2VvLm9yZw&url=https%3A%2F%2Fcourses.spatialthoughts.com%2Fpyqgis-masterclass.html%23writing-standalone-python-scripts&u=8747767&signature=20c626c64e491be4">https://courses.spatialthoughts.com/pyqgis-masterclass.html#writing-standalone-python-scripts</a><br clear="all"><div><div dir="ltr" class="gmail_signature"><div dir="ltr">---<div>Ujaval Gandhi</div><div>Spatial Thoughts</div><div><a href="https://mailtrack.io/l/0759060cce5dfd1efebd0082630133103cf22d2a?w=cWdpcy11c2VyQGxpc3RzLm9zZ2VvLm9yZw&url=http%3A%2F%2Fwww.spatialthoughts.com&u=8747767&signature=8d3fc0a0dc5f7c99" target="_blank">www.spatialthoughts.com</a></div><div><br></div></div></div></div><br></div><br><img width="0" height="0" class="mailtrack-img" alt="" style="display:flex" src="https://mailtrack.io/trace/mail/w/cWdpcy11c2VyQGxpc3RzLm9zZ2VvLm9yZw/71f03279c11aa7ca7990b7220fa43958294393a0.png?u=8747767" ><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Jul 26, 2024 at 4:16 PM Roland Berger via QGIS-User <qgis-user@lists.osgeo.org> wrote:<br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><u></u>

  

    
  
  <div>
    Hi <br>
    <br>
    I would like to add a virtual field to a PostGIS vector layer with
    PyQGIS with standalone scripts
(<a href="https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/intro.html#python-applications" target="_blank">https://docs.qgis.org/3.34/<wbr>en/docs/pyqgis_developer_<wbr>cookbook/intro.html#python-<wbr>applications</a>)<br>
    All examples I have found so far with google, chatgpt4 and JetBrains
    AI Assistant gave me solutions that do not work. They only work in
    the Python Console inside QGIS.<br>
    Does anybody know of a solution that works with PyQGIS with
    standalone scripts?<br>
    See below what I have tried so far in different variations.<br>
    <br>
    Thanks for any help <br>
    Roland<br>
    <br>
    Example created by chatgpt:<br>
    ...<br>
    <pre>field_name = "virtual_field"</pre>
    <pre>expression = '"attribute_name" * 2'  # Example expression</pre>
    <pre># Create and add the virtual field</pre>
    <pre>layer.startEditing()</pre>
    <pre>layer.addExpressionField(<wbr>expression, QgsField(field_name, QVariant.Double))</pre>
    <pre>layer.commitChanges()</pre>
    ...<br>
    <br>
    <br>
  </div>

______________________________<wbr>_________________<br>
QGIS-User mailing list<br>
<a href="mailto:QGIS-User@lists.osgeo.org" target="_blank">QGIS-User@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailma<wbr>n/listinfo/qgis-user</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailma<wbr>n/listinfo/qgis-user</a><br>
</blockquote></div>