'How to check if the environment variable "PROJ_LIB" is defined and how to unset it ? (PyQGIS Standalone Script Executer)

I just tried the standalone PyQGIS application by running the custom script "Proximity.py"* in a VS Code project without the need of a GUI (such as QGIS).

But, when I run the python-program I get the following message:

proj_create_from_database: C:\Program Files\PostgreSQL\14\share\contrib\postgis-3.2\proj\proj.db contains DATABASE.LAYOUT.VERSION.MINOR = 0 whereas a number >= 2 is expected. It comes from another PROJ installation. (see also: Error Message after launching the configuration (launch.json) from VS Code (when pressing F5))

I'm trying this online example with the following installations:

  • PostgreSQL 14
  • Python39
  • .vscode\extensions\ms-python.python-2022.4.1\pythonFiles\lib\python\debugpy\launcher
  • osgeo4w-setup.exe (including QGIS LTR)

I read that there is a solution by undefining [PROJ_LIB] before importing pyproj or osgeo: del os.environ ['PROJ_LIB'] as described under this link. If this is also supposed to be the correct solution in this case, can someone help me with step-by-step instructions (for dummies)?

. * The "Proximity.py" script is a pyqgis standalone example from "https://github.com/MarByteBeep/pyqgis-standalone"



Solution 1:[1]

Finally, I got a solution to be able to run the "standalone PyQGIS"* example "Proximity" (provided by MarByteBeep).

This solution was possible without needing to launch the configuration file "launch.json" as above described. And so, avoiding the need to make any configuration to the environment variable "PROJ_LIB" by trying to circumvent the above issue.
I just first added the following two code-lines (see here line 2 and 3) in the python file "main.py" so as to be able to use the plugin "PROCESSING" (initially line 8 of the "main.py" file), then I store it and finally I ran it.

Line 1: from qgis.core import

Line 2: import sys
Line 3: sys.path.append('C:\Program Files\QGIS 3.24.1\apps\qgis\python\plugins')

Line 4: qgs = QgsApplication([], False)
Line 5: ...

The Proximity example is based on the answer of "Mar Tjin" to the following Question: "Looking for manual on how to properly setup standalone PyQGIS without GUI"

. * By "Standalone PyQGIS" I refer to code/scripts that can be run outside the QGIS-GUI (=> QGIS-Desktop/Server Application). In my case under the external Editor VS Code

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1