1

To run a python script (example file "standalone.py") with module qgis.core I have to use a specific bat file (that call the py file!) with the following environment!

"rem Call qgis_process-qgis.bat file to set QGIS Windows environment variables
call "C:\ProgrammiAC\QGIS3.28\bin\qgis_process-qgis-ltr.bat"
rem Append QGIS Python Plugins directory to PYTHONPATH
set PYTHONPATH=%PYTHONPATH%;%QGIS_PREFIX_PATH%/python/plugins
rem Launch Python job
python "P:\path\standalone.py"
rem Pause Windows batch job
pause"

Otherwise, qgis.core is not found as happen also if I run python3.exe in C:\ProgrammiAC\QGIS3.28\apps\Python39.

There is a way to allow python3.exe or other python interpreter to find qgis.core or other qgis specific QGIS module WITHOUT running before the specific bat file?

I want that every time that I run python3.exe it works without adding nothing. Now when I did in python3.exe "import qgis.core"

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'qgis'

I assume that is the problem that force me every time to use the bat before python script if I use this interpreter. The interpreter is not able to find independently the module of qgis that are here: C:\ProgrammiAC\QGIS3.28\apps\qgis-ltr\python

Obviously the solution doesn't have to have a negative impact on the python console in QGIS.

The reason of this question is that at the moment I am able to write and execute python for standalone script of qgis only after running bat file and I find annoying to remember every time to run the bat.

ADDED 21.06

Now I did how @Pieter suggested in https://gis.stackexchange.com/a/482613/165037 inserting the variable found in the abovementioned bat file that works when launched:

import os
os.environ['OSGEO4W_ROOT']="C:\ProgrammiAC\QGIS3.28"
os.environ['QGIS_PREFIX_PATH']=f"{os.environ['OSGEO4W_ROOT']}\apps\qgis-ltr"
os.environ['PYTHONPATH']= f"{os.environ['PYTHONPATH']};{os.environ['QGIS_PREFIX_PATH']}\python\plugins"
os.environ['QT_PLUGIN_PATH']=f"{os.environ['OSGEO4W_ROOT']}\apps\qgis-ltr\qtplugins;{os.environ['OSGEO4W_ROOT']}\apps\Qt5\plugins"
os.environ['PATH']=f"{os.environ['OSGEO4W_ROOT']}\apps\qgis-ltr\bin;{os.environ['QGIS_PREFIX_PATH']}"
import qgis.core

The code gives no error, but when it have to import qgis.core the answer is always the same: No module named 'qgis'

To note: @user2856 tell me to use even environment variable in qt5_env.bat and py3_env.bat but I cannot find them in my folders. So, I did not used them.

So, I am able to give the variables using the bat file, but I am not able to write a python script that add the same variables even if the script does not give errors!!

I did not surrended and I use a script from chatgpt:

import sys
import os
# Path to the QGIS installation
QGIS_PREFIX_PATH = "C:\ProgrammiAC\QGIS3.28" # Modifica questo percorso in base alla tua installazione
# Set the QGIS_PREFIX_PATH environment variable
os.environ['QGIS_PREFIX_PATH'] = QGIS_PREFIX_PATH
# Check if PYTHONPATH is already set, if not set it to an empty string
pythonpath = os.environ.get('PYTHONPATH', '')
# Concatenate the QGIS paths to PYTHONPATH
os.environ['PYTHONPATH'] = f"{pythonpath};{os.environ['QGIS_PREFIX_PATH']}/apps/qgis-ltr/python;{os.environ['QGIS_PREFIX_PATH']}/apps/qgis-ltr/python/plugins"
# Add the QGIS Python module paths to sys.path
sys.path.append(os.path.join(QGIS_PREFIX_PATH, 'apps', 'qgis-ltr', 'python'))
sys.path.append(os.path.join(QGIS_PREFIX_PATH, 'apps', 'qgis-ltr', 'python', 'plugins'))

this time it opened automatically a file called "QtCore.py" that stops because "DLL load failed while importing QtCore". It remains unexplained why with the bat file there are no problems, with python code there are these problems.

asked Jun 13, 2024 at 13:44

1 Answer 1

2

You can set the environment variable in your python script before you import qgis.core.

E.g. for the environment variable in your code above:

rem Append QGIS Python Plugins directory to PYTHONPATH
set PYTHONPATH=%PYTHONPATH%;%QGIS_PREFIX_PATH%/python/plugins

Can be replace by something like this in your python script (untested):

import os
# Append QGIS Python Plugins directory to PYTHONPATH
os.environ["PYTHONPATH"]=f"{os.environ['PYTHONPATH']};{os.environ['QGIS_PREFIX_PATH']}/python/plugins"
import qgis.core
...
answered Jun 13, 2024 at 14:22
8
  • it is not able due to 'QGIS_PREFIX_PATH' in your script. But all the other attempt with python code did not work. Only bat file run before are able @Pieter Commented Jun 13, 2024 at 14:55
  • I assumed the comments in your sample above were what is in the .bat file, apparently not. But if you put the same initialization in python before importing as is in the .bat file it should work... Or, you could call the bat file with os.system()? Commented Jun 13, 2024 at 15:30
  • @simone100 you also need to set all the other environment variables (e.g. QGIS_PREFIX_PATH) which would have been set on line 2 of your .bat which calls "C:\ProgrammiAC\QGIS3.28\bin\qgis_process-qgis-ltr.bat". So open qgis_process-qgis-ltr.bat, work out what env variables it sets, and use os.environ to set those as well. In the same order. Commented Jun 13, 2024 at 22:38
  • 2
    Probably the .bat files only contain commands to set environment variables... so all SET NAME = VALUE calls need to be replaced by os.environ["NAME"]=VALUE in python as you can see an example for in my answer... Commented Jun 14, 2024 at 14:03
  • 1
    @simone100 I did not post an answer, just a comment on the original answer to point you in the right direction. Just use os.environ to populate all the env vars that are set in your .bat file and the .bat files called by it. Commented Jun 15, 2024 at 1:58

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.