Skip to content

Instantly share code, notes, and snippets.

@brccabral
Last active October 13, 2022 01:49
Show Gist options
  • Save brccabral/a6edf19305092ec5c0e277a88054c368 to your computer and use it in GitHub Desktop.
Save brccabral/a6edf19305092ec5c0e277a88054c368 to your computer and use it in GitHub Desktop.
QGIS and Python

QGIS and Python

QGIS comes with its own Python, but it is missing Tkinter DLLs.
Create a new .bat file to add PYTHONPATH to environment variables. Check if your installation is qgis or qgis-ltr.

C:\OSGeo4W\etc\ini\user.bat

SET PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%OSGEO4W_ROOT%\apps\qgis\python\plugins
SET PATH=%PATH%;%LOCALAPPDATA%\Programs\Microsoft VS Code

Also, if needed to use TKinter, QGIS' python doesn't come with it, but we can grab files from our Python and copy them to QGIS location.

From %USERPROFILE%\AppData\Local\Programs\Python\Python39\DLLs, copy tcl86t.dll, tk86t.dll, _tkinter.pyd and paste them into C:\OSGeo4W\apps\Python39\DLLs.

C:\OSGeo4W\apps\Python39\DLLs\tcl86t.dll
C:\OSGeo4W\apps\Python39\DLLs\tk86t.dll
C:\OSGeo4W\apps\Python39\DLLs\_tkinter.pyd
C:\OSGeo4W\apps\Python39\DLLs\sqlite3.dll

Also, copy the whole folder %USERPROFILE%\AppData\Local\Programs\Python\Python39\tcl and paste it in C:\OSGeo4W\apps\Python39

C:\OSGeo4W\apps\Python39\tcl

Do not start VSCode from start menu or any shortcut.
First, run C:\OSGeo4W\OSGeo4W.bat so it can open a command prompt with all the environment variables needed, including the newly created user.bat above.
Then, from this command prompt start your VSCode code, it will open VSCode with the OSGeo4W environment variables.

:: pay attention if qgis-ltr or just qgis
:: edit file C:\OSGeo4W\etc\ini\python3.bat
SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python39
SET PYTHONUTF8=1
PATH %OSGEO4W_ROOT%\apps\Python39\Scripts;%OSGEO4W_ROOT%\apps\qgis-ltr\bin;%PATH%
:: or create a new .bat at same location C:\OSGeo4W\etc\ini and add this line
SET PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis-ltr\python;%OSGEO4W_ROOT%\apps\qgis-ltr\python\plugins
:: file C:\OSGeo4W\OSGeo4W.bat loads all .bat scripts inside C:\OSGeo4W\etc\ini
import logging
logging.basicConfig(format='%(levelname)s - %(asctime)s - %(name)s - %(message)s',
datefmt='%H:%M:%S', level=logging.INFO, filename='C:\\path\\neighbors.log')
from qgis.core import QgsApplication
#QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis-ltr", True) # QgsApplication.prefixPath()
QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis", True) # QgsApplication.prefixPath()
app1 = QgsApplication([], False)
app1.initQgis()
from processing.core.Processing import Processing
proc = Processing()
proc.initialize()
# Do something, for example
for alg in QgsApplication.processingRegistry().algorithms():
print(alg.id(), "->", alg.displayName())
app1.exitQgis()
import tkinter as tk
import os
root = tk.Tk()
root.title("TKinter GUI")
root.geometry("400x400")
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment