We (Git Hub's co-pilot and I) are attempting to automate several processes using Python. One step in the process requires that I create and manipulate QGIS projects. Apparently this step requires that I create a venv to access PyQGIS.
Despite many iterations I am stuck at point where the code errors and returns, in part, "Failed to load qgis_core.dll: Could not find module 'C:\Program Files\QGIS 3.34.11\apps\qgis-ltr\bin\qgis_core.dll' (or one of its dependencies)". The dll definitely exists in the specified location.
The full terminal output is below. The code block follows.
Terminal output:
"PS C:\Users\tobin\Documents\Python> & C:/Users/tobin/AppData/Local/Programs/Python/Python311/python.exe c:/Users/tobin/Documents/Python/verify_core_import.py
QGIS_PREFIX_PATH: C:\Program Files\QGIS 3.34.11\apps\qgis-ltr\python\qgis
PATH: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files (x86)\AOMEI\AOMEI Backupper\7.2.0;C:\Program Files\PostgreSQL\16\bin;C:\Users\tobin\AppData\Local\Programs\Python\;C:\Program Files\PuTTY\;C:\Users\tobin\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\tobin\AppData\Local\Programs\Python\Python311\;C:\Users\tobin\AppData\Local\Microsoft\WindowsApps;C:\blp\DAPI;C:\blp\DAPI\DDE;C:\Users\tobin\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\tobin\.dotnet\tools;C:\Program Files\PostgreSQL\16\bin;;C:\Program Files\QGIS 3.34.11\apps\qgis-ltr\bin
PYTHONPATH: C:\Program Files\QGIS 3.34.11\apps\qgis-ltr\python\qgis;C:\Users\tobin\Documents\Python\qgis_env\Lib\site-packages
GDAL_DATA: C:\Program Files\QGIS 3.34.11\share\gdal
PROJ_LIB: C:\Program Files\QGIS 3.34.11\share\proj
sys.path: ['c:\\Users\\tobin\\Documents\\Python', 'C:\\Users\\tobin\\AppData\\Local\\Programs\\Python\\Python311\\python311.zip', 'C:\\Users\\tobin\\AppData\\Local\\Programs\\Python\\Python311\\DLLs', 'C:\\Users\\tobin\\AppData\\Local\\Programs\\Python\\Python311\\Lib', 'C:\\Users\\tobin\\AppData\\Local\\Programs\\Python\\Python311', 'C:\\Users\\tobin\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages', 'C:\\Users\\tobin\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\win32', 'C:\\Users\\tobin\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\win32\\lib', 'C:\\Users\\tobin\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\Pythonwin', 'C:\\Program Files\\QGIS 3.34.11\\apps\\qgis-ltr\\python\\qgis', 'C:\\Users\\tobin\\Documents\\Python\\qgis_env\\Lib\\site-packages']
Failed to import qgis.core: No module named 'qgis'
Contents of qgis_python_qgis_path: ['3d', 'analysis', 'core', 'gui', 'processing', 'PyQt', 'testing', 'user.py', 'utils.py', '_3d.pyd', '_3d.pyi', '_analysis.pyd', '_analysis.pyi', '_core.pyd', '_core.pyi', '_gui.pyd', '_gui.pyi', '__init__.py']
Contents of qgis_bin_path: ['qgis.reg', 'qgis.reg.tmpl', 'qgisgrass8.dll', 'qgispython.dll', 'qgis_3d.dll', 'qgis_analysis.dll', 'qgis_app.dll', 'qgis_core.dll', 'qgis_gui.dll', 'qgis_native.dll', 'qgis_process.exe']
Failed to load qgis_core.dll: Could not find module 'C:\Program Files\QGIS 3.34.11\apps\qgis-ltr\bin\qgis_core.dll' (or one of its dependencies). Try using the full path with constructor syntax."
# verify_core_import.py
import os
import sys
def setup_qgis_environment():
# Set environment variables for QGIS
global qgis_python_qgis_path, qgis_bin_path
qgis_python_qgis_path = r"C:\Program Files\QGIS 3.34.11\apps\qgis-ltr\python\qgis"
qgis_bin_path = r"C:\Program Files\QGIS 3.34.11\apps\qgis-ltr\bin"
venv_site_packages = r"C:\Users\tobin\Documents\Python\qgis_env\Lib\site-packages"
os.environ['QGIS_PREFIX_PATH'] = qgis_python_qgis_path
os.environ['PATH'] += os.pathsep + qgis_bin_path
os.environ['PYTHONPATH'] = os.pathsep.join([qgis_python_qgis_path, venv_site_packages])
# Set additional environment variables
os.environ['GDAL_DATA'] = r"C:\Program Files\QGIS 3.34.11\share\gdal"
os.environ['PROJ_LIB'] = r"C:\Program Files\QGIS 3.34.11\share\proj"
# Manually append to sys.path
sys.path.append(qgis_python_qgis_path)
sys.path.append(venv_site_packages)
if __name__ == "__main__":
# Set up QGIS environment
setup_qgis_environment()
# Print environment variables for debugging
print("QGIS_PREFIX_PATH:", os.environ.get('QGIS_PREFIX_PATH'))
print("PATH:", os.environ.get('PATH'))
print("PYTHONPATH:", os.environ.get('PYTHONPATH'))
print("GDAL_DATA:", os.environ.get('GDAL_DATA'))
print("PROJ_LIB:", os.environ.get('PROJ_LIB'))
# Print sys.path for debugging
print("sys.path:", sys.path)
# Verify import of qgis.core
try:
from qgis.core import QgsApplication
print("Successfully imported qgis.core")
except ImportError as e:
print(f"Failed to import qgis.core: {e}")
print("Contents of qgis_python_qgis_path:", os.listdir(qgis_python_qgis_path))
# Check for DLL dependencies
print("Contents of qgis_bin_path:", os.listdir(qgis_bin_path))
# Check if qgis_core.dll can be loaded
try:
import ctypes
ctypes.CDLL(os.path.join(qgis_bin_path, 'qgis_core.dll'))
print("Successfully loaded qgis_core.dll")
except OSError as dll_error:
print(f"Failed to load qgis_core.dll: {dll_error}")