pythonwindowsqgispython-venvpyqgis

QGIS python returning non-zero exit status 1 trying to create a venv


I'm trying to setup a venv using the Python interpreter which ships with QGIS 3.22.6 according to this blog post here. However, when I execute the specific command, the following error occurs:

C:\Source\experiments\qgistest>C:\QGIS\bin\python-qgis-ltr.bat -m venv .venv
Error: Command '['C:\\Source\\experiments\\qgistest\\.venv\\Scripts\\python.exe', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

Similiar questions here on SO indicate that the venv package could not be present, or that it's an error related to ensurepip, however as for the former, the specified .venv folder does get created (partially) and ensurepip executes just aswell. Sadly I don't get any further indicators what might be the issue here.


Solution

  • DISCLAIMER: I'm not very familiar with how virtual environments work or QGIS builds, but through trial and error I managed to get the following to work on Windows 10.

    1. Download/Install standalone QGIS (OSGeo builds don't seem to be working for venv)
    2. Create a custom setup batch file. Start with python-qgis.bat (which is distributed in /bin), then add python/plugins and the QGIS DLLs paths to PYTHONPATH (see my batch script below). You may need more environment variables here depending on how complicated your build is (e.g., using GRASS).
    3. Run the batch file and test that python builds and runs correctly (e.g., import processing)
    4. Build the virtual environment without pip* python -m venv --without-pip .venv/myvenv
    5. Edit the venv config file (pyvenv.cfg) and set include-system-site-packages = true
    6. Activate the venv and test that python builds and runs. You should be able to install new packages into this venv via pip (these packages will show up in myvenv/Lib/site-packages)
    set IDIR=C:\Program Files\QGIS 3.26.0
    set QREL=qgis
    set PYVER=Python39
    
    
    REM adapted from C:\Program Files\QGIS 3.26.0\bin\python-qgis.bat
    @echo off
    call "%IDIR%\bin\o4w_env.bat"
    @echo off
    path %OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
    set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
    set GDAL_FILENAME_IS_UTF8=YES
    rem Set VSI cache to be used as buffer, see #6448
    set VSI_CACHE=TRUE
    set VSI_CACHE_SIZE=1000000
    set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
    set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%
    
    REM ==========================================
    REM add QGIS dlls
    REM ==========================================
    set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\%QREL%\python\plugins
    set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\%PYVER%\
    set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\%PYVER%\DLLs
    set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\%PYVER%\lib
    set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\%PYVER%\lib\site-packages
    

    I assume these workarounds are required because of the python 3.9 DLL errors mentioned by the commenter.

    *NOTE: I'm not sure why this workaround is required... as pip seems to work fine. Also not sure why this workaround doesn't work on OSGEO builds.