QGIS – Environment Variables for Plugin Development on Windows

pyqgisqgisqgis-plugins

I want to start learning QGIS plugin development, and I'm trying to get set up for Python 3, PyQt5 and QGIS 3.

I've installed QGIS 2.99, qt5-devel and setuptools via OSGeo4W. Now I get stuck on the environment config, following and modifying this quick guide to PyQGIS on Windows. I've modified it to:

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\apps\grass\grass-7.2.2\etc\env.bat
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis-dev\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass-7.2.2\lib
path %OSGEO4W_ROOT%\apps\Python36;%OSGEO4W_ROOT%\apps\Python36\Scripts;%PATH%
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python36
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis-dev\python
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python36\Lib\site-packages
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis-dev
cmd.exe

I've changed references to %OSGEO4W_ROOT%\apps\qgis to %OSGEO4W_ROOT%\apps\qgis-dev, and %OSGEO4W_ROOT%\apps\Python27 to %OSGEO4W_ROOT%\apps\Python36.

Since "%OSGEO4W_ROOT%"\bin\o4w_env.bat includes etc\ini\python-core.bat, which does SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python27 and PATH %OSGEO4W_ROOT%\apps\Python27\Scripts;%PATH%, I've just added Python 3.6 explicitly to the path and set PYTHONHOME to 3.6 as well, but that's ugly. Is there a better way?

It results in these env variables:

echo %PATH%
C:\OSGEO4~1\apps\Python36;C:\OSGEO4~1\apps\Python36\Scripts;C:\OSGEO4~1\apps\Python27\Scripts;C:\OSGEO4~1\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32\WBem;C:\OSGEO4~1\apps\qgis-dev\bin;C:\OSGEO4~1\apps\grass\grass-7.2.2\lib
echo %PYTHONHOME%
C:\OSGEO4~1\apps\Python36
echo %PYTHONPATH%
;C:\OSGEO4~1\apps\qgis-dev\python;C:\OSGEO4~1\apps\Python36\Lib\site-packages

With the above I can run the correct python version, but I run into this:

E:\>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import qgis.core
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\OSGEO4~1\apps\qgis-dev\python\qgis\__init__.py", line 71, in <module>
    from qgis.PyQt import QtCore
  File "C:\OSGEO4~1\apps\qgis-dev\python\qgis\PyQt\QtCore.py", line 1, in <module>
    from PyQt5.QtCore import *
ImportError: DLL load failed: The specified module could not be found.

Best Answer

This is my .bat for QGIS 3

@ECHO OFF 

set OSGEO4W_ROOT=D:\OSGeo4W64

set PATH=%OSGEO4W_ROOT%\bin;%PATH% 
set PATH=%PATH%;%OSGEO4W_ROOT%\apps\qgis\bin

@echo off 
call "%OSGEO4W_ROOT%\bin\o4w_env.bat" 
call "%OSGEO4W_ROOT%\bin\qt5_env.bat" 
call "%OSGEO4W_ROOT%\bin\py3_env.bat" 
@echo off 
path %OSGEO4W_ROOT%\apps\qgis-dev\bin;%OSGEO4W_ROOT%\apps\grass\grass-7.2.2\lib;%OSGEO4W_ROOT%\apps\grass\grass-7.2.2\bin;%PATH%

cd /d %~dp0

::start cmd

And for compile .ui in a QGIS 3 Plugin https://gis.stackexchange.com/a/260886/49538

Related Question