Skip to content

fix(qgis): validate Python interpreters before venv creation#86

Merged
giswqs merged 5 commits into
mainfrom
fix/macos-qgis-bundle-python-validation
May 8, 2026
Merged

fix(qgis): validate Python interpreters before venv creation#86
giswqs merged 5 commits into
mainfrom
fix/macos-qgis-bundle-python-validation

Conversation

@giswqs

@giswqs giswqs commented May 8, 2026

Copy link
Copy Markdown
Member

Summary

  • Probe Python interpreter candidates with a short subprocess so unstartable binaries (e.g. the official macOS QGIS bundle's python3.x failing with "No module named 'encodings'") are skipped instead of being passed to uv or venv.
  • When no startable interpreter is found, ask uv to resolve the matching Python version spec so venv creation can still succeed on affected macOS installations.
  • Surface rejected candidates and their reasons in the RuntimeError message to make diagnosis easier.

Test plan

  • pre-commit run --all-files
  • pytest qgis_geoagent/tests/test_settings_diagnostics.py
  • Manual: trigger venv creation on an official macOS QGIS app bundle and confirm it falls back to uv with the matching version

The official macOS QGIS app bundle ships a python3.x binary whose embedded
prefix points at the build machine, so it fails before startup with "No
module named 'encodings'". Probe candidates with a short subprocess and
fall back to letting uv resolve the matching Python version when no
startable interpreter is found.
Copilot AI review requested due to automatic review settings May 8, 2026 19:47
@github-actions

github-actions Bot commented May 8, 2026

Copy link
Copy Markdown

@github-actions github-actions Bot temporarily deployed to pull request May 8, 2026 19:48 Inactive

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves QGIS plugin virtual-environment creation reliability (especially on macOS QGIS.app bundles) by validating Python interpreter candidates before using them, and by allowing uv to resolve a matching Python version when the bundled interpreter cannot start.

Changes:

  • Add a “probe” check to verify Python interpreter candidates can start and match the current QGIS Python major/minor.
  • Update interpreter selection to skip unstartable candidates and include rejected-candidate reasons in failure messages.
  • Adjust create_venv() to fall back to uv venv --python <major.minor> when no startable interpreter path is available, and add tests for these scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
qgis_geoagent/open_geoagent/deps_manager.py Adds interpreter usability probing, improves candidate selection diagnostics, and updates create_venv() to let uv resolve a Python version when needed.
qgis_geoagent/tests/test_settings_diagnostics.py Updates/extends tests to cover skipping broken macOS bundle Python and uv version-resolution fallback behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread qgis_geoagent/open_geoagent/deps_manager.py Outdated
Comment thread qgis_geoagent/tests/test_settings_diagnostics.py
- Reword the no-Python-found RuntimeError to say "sys.executable is not a
  usable Python interpreter" since the branch is now reachable when
  sys.executable is Python but unstartable or the wrong major.minor.
- Stub _python_executable_usable in the macOS bundle tests to return an
  empty reason on success, matching the real implementation contract.
@github-actions github-actions Bot temporarily deployed to pull request May 8, 2026 19:55 Inactive
- Updated `venv_exists` to accept an optional `venv_dir` argument for improved flexibility.
- Introduced `venv_python_usable` to validate the usability of the Python executable in the virtual environment.
- Added `_ensure_usable_venv` to recreate stale or broken virtual environments.
- Enhanced `create_venv` to handle errors more gracefully and provide clearer feedback on failures.
- Updated `install_packages` to retry with pip if uv installation fails.
- Added tests to ensure proper handling of broken virtual environments and fallback mechanisms during package installation.
@github-actions github-actions Bot temporarily deployed to pull request May 8, 2026 20:04 Inactive
- Introduced `_is_macos_qgis_app_bundle_python` to identify Python binaries within QGIS macOS app bundles.
- Updated `_python_executable_usable` to reject app bundle Python for virtual environment creation, ensuring safer environment management.
- Added a test to verify that QGIS app bundle Python is correctly identified and rejected for venv usage.
@github-actions github-actions Bot temporarily deployed to pull request May 8, 2026 20:08 Inactive
@github-actions github-actions Bot temporarily deployed to pull request May 8, 2026 20:12 Inactive
@giswqs giswqs merged commit 7994ea7 into main May 8, 2026
9 checks passed
@giswqs giswqs deleted the fix/macos-qgis-bundle-python-validation branch May 8, 2026 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants