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.
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.
import processing)python -m venv --without-pip .venv/myvenvinclude-system-site-packages = truemyvenv/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.