From 84ea18df22fd028385c8702b866d919fc83e64ee Mon Sep 17 00:00:00 2001 From: Taylor Steinberg Date: Mon, 9 Nov 2026 13:52:59 -0500 Subject: [PATCH 1/2] fix: import without the uv Python module --- docs/CHANGELOG.md | 4 ++++ rsconnect/environment.py | 8 ++++++-- tests/test_environment.py | 23 +++++++++++++++-------- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ff25da01..62516303 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Imports in environments that have the `uv` executable but not its Python module, such as conda-forge, instead of failing with `ModuleNotFoundError: No module named 'uv'`. Finds `uv` on `PATH` instead. + ## [1.33.0] - 2026-11-09 ### Added diff --git a/rsconnect/environment.py b/rsconnect/environment.py index 5a0286be..4c1b8113 100644 --- a/rsconnect/environment.py +++ b/rsconnect/environment.py @@ -15,7 +15,6 @@ import pathlib import os.path import enum -import uv from . import pyproject from .subprocesses import inspect_environment @@ -273,7 +272,12 @@ def _inspect_environment( def _uv_bin() -> str: try: - return uv.find_uv_bin() + # conda-forge's uv package installs only the executable, not this module. + from uv import find_uv_bin + except ImportError: + return "uv" + try: + return find_uv_bin() except FileNotFoundError: return "uv" diff --git a/tests/test_environment.py b/tests/test_environment.py index c8a0a8c0..25a32e6c 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -432,7 +432,7 @@ def test_uv_lock_export(tmp_path, monkeypatch, fallback, lock_location): python = env_dir / ("Scripts/python.exe" if os.name == "nt" else "bin/python") if fallback: monkeypatch.setenv("PATH", os.path.dirname(uv_path)) - monkeypatch.setattr(rsconnect.environment.uv, "find_uv_bin", missing_bundled_uv) + monkeypatch.setattr(uv, "find_uv_bin", missing_bundled_uv) else: monkeypatch.setenv("PATH", "") monkeypatch.chdir(project_dir) @@ -732,11 +732,15 @@ def test_project_environment_lookup_includes_ancestors_outside_content_project(t def test_inspect_environment_without_bundled_uv(monkeypatch): - monkeypatch.setattr(rsconnect.environment.uv, "find_uv_bin", missing_bundled_uv) + monkeypatch.setattr(uv, "find_uv_bin", missing_bundled_uv) environment = Environment._inspect_environment(sys.executable, get_dir("pip1")) assert environment.source == "file" +def test_cli_imports_without_uv_module(): + subprocess.run([sys.executable, "-c", "import sys; sys.modules['uv'] = None; import rsconnect.main"], check=True) + + def test_inspect_environment_reports_invalid_pyproject(tmp_path): (tmp_path / "pyproject.toml").write_text("[project\n") with pytest.raises(RSConnectException, match="Error reading pyproject.toml"): @@ -875,8 +879,8 @@ def test_inspection_when_rsconnect_is_importable_only_from_pythonpath(tmp_path): assert json.loads(result.stdout.splitlines()[-1]).splitlines() == ["project-package==1.0"] -@pytest.mark.parametrize("fallback", [False, True]) -def test_uv_freeze_filters_distributions_and_rewrites_editable_file_urls(tmp_path, monkeypatch, fallback): +@pytest.mark.parametrize("uv_source", ["bundled", "path_without_bundled_binary", "path_without_uv_module"]) +def test_uv_freeze_filters_distributions_and_rewrites_editable_file_urls(tmp_path, monkeypatch, uv_source): uv_path = uv.find_uv_bin() env_dir = tmp_path / "env" subprocess.run([uv_path, "venv", "--python", sys.executable, str(env_dir)], check=True) @@ -890,11 +894,14 @@ def test_uv_freeze_filters_distributions_and_rewrites_editable_file_urls(tmp_pat dist = site / f"{name.replace('-', '_')}-1.0.dist-info" dist.mkdir() (dist / "METADATA").write_text(f"Metadata-Version: 2.1\nName: {name}\nVersion: 1.0\n") - if fallback: - monkeypatch.setenv("PATH", os.path.dirname(uv_path)) - monkeypatch.setattr(rsconnect.environment.uv, "find_uv_bin", missing_bundled_uv) - else: + if uv_source == "bundled": monkeypatch.setenv("PATH", "") + else: + monkeypatch.setenv("PATH", os.path.dirname(uv_path)) + if uv_source == "path_without_bundled_binary": + monkeypatch.setattr(uv, "find_uv_bin", missing_bundled_uv) + elif uv_source == "path_without_uv_module": + monkeypatch.setitem(sys.modules, "uv", None) environment = Environment._inspect_environment(str(python), str(tmp_path), requirements_file=None) assert "rsconnect-utils==1.0" in environment.contents.splitlines() From 286ab94f37a0b88e50c475f3ba8f429e10b85fb1 Mon Sep 17 00:00:00 2001 From: Taylor Steinberg Date: Mon, 9 Nov 2026 13:53:20 -0500 Subject: [PATCH 2/2] docs: link changelog entry to #938 Co-Authored-By: Claude Opus 5.5 --- docs/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 62516303..4adb5f30 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Imports in environments that have the `uv` executable but not its Python module, such as conda-forge, instead of failing with `ModuleNotFoundError: No module named 'uv'`. Finds `uv` on `PATH` instead. +- Imports in environments that have the `uv` executable but not its Python module, such as conda-forge, instead of failing with `ModuleNotFoundError: No module named 'uv'`. Finds `uv` on `PATH` instead. [#938](https://github.com/posit-dev/rsconnect-python/pull/938) ## [1.33.0] - 2026-11-09