Python: Fix venv isolation from user-site and base-install site-packages [STUD-81085] - #596
Python: Fix venv isolation from user-site and base-install site-packages [STUD-81085]#596viogroza wants to merge 1 commit into
Conversation
…ges [STUD-81085] Python Scope's embedded interpreter never goes through CPython's own venv activation path (site.py's venv() function, which only triggers for a normally-launched <venv>/Scripts/python.exe), so pointing Path at a venv left both the PEP-370 user-site directory and the base install's own site-packages on sys.path unfiltered. A native package (e.g. pywin32) installed with a different build in either location could resolve its Python module from one and its native DLL dependency from the other, mismatched, copy, surfacing as "DLL load failed while importing win32api: The specified procedure could not be found." Fixes user-site and base-install leakage via PythonEngine.SetNoSiteFlag() (gated on whether the venv requests --system-site-packages), with Controller.ClearUserSiteEnvironmentOverride guarding against an ambient PYTHONNOUSERSITE leaking in from the host's own environment. Also fixes PythonHome being pointed at the venv root instead of its declared base install (breaks stdlib resolution entirely), tightens venv detection against false positives, and adds a version cross-check between a venv and the Python library actually being loaded. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
There was a problem hiding this comment.
Pull request overview
This PR hardens the Python activity pack’s handling of virtual environments (venvs) when using an embedded interpreter, ensuring sys.path is correctly isolated from PEP-370 user-site and (when appropriate) the base install’s site-packages, and surfacing clearer errors for misconfigured venv/library pairings.
Changes:
- Adds shared venv detection (
pyvenv.cfg) and uses it to drive runtime initialization behavior. - Updates engine initialization to set an appropriate
PythonHome, gatePy_NoSiteFlag/SetNoSiteFlag()based on venv flags, and preservesitecustomize.pyexecution. - Adds validation and regression tests for venv detection, version mismatch, user-site isolation, base-site leakage, and ambient
PYTHONNOUSERSITEbehavior.
Reviewed changes
Copilot reviewed 10 out of 11 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| Activities/Python/UiPath.Python/Properties/UiPath.Python.resx | Adds localized error text for venv/library version mismatch. |
| Activities/Python/UiPath.Python/Properties/UiPath.Python.Designer.cs | Adds generated accessor for the new localized resource. |
| Activities/Python/UiPath.Python/Properties/AssemblyInfo.cs | Grants internals visibility to UiPath.Python.Tests for new regression coverage. |
| Activities/Python/UiPath.Python/Impl/VenvDetection.cs | Introduces shared, bounded venv detection + config parsing (pyvenv.cfg). |
| Activities/Python/UiPath.Python/Impl/OutOfProcessEngine.cs | Plumbs venv flag into host spawning to clear ambient PYTHONNOUSERSITE when needed. |
| Activities/Python/UiPath.Python/Impl/Engine.cs | Fixes PythonHome resolution for venvs, applies SetNoSiteFlag() for default venvs, and preserves sitecustomize.py. |
| Activities/Python/UiPath.Python/EngineProvider.cs | Adds venv-vs-library version cross-check during installation validation. |
| Activities/Python/UiPath.Python.Tests/VenvVersionValidationTests.cs | Regression tests for upfront venv/library version mismatch handling. |
| Activities/Python/UiPath.Python.Tests/VenvUserSiteIsolationTests.cs | Regression coverage for user-site suppression, base-site leakage, sitecustomize preservation, and ambient env-var behavior. |
| Activities/Python/UiPath.Python.Tests/VenvDetectionTests.cs | Fast unit tests for venv detection shapes and false-positive avoidance. |
| Activities/Python/Shared/UiPath.Shared.Service/Client/Controller.cs | Adds child-process environment scrubbing for PYTHONNOUSERSITE when requested. |
Files not reviewed (1)
- Activities/Python/UiPath.Python/Properties/UiPath.Python.Designer.cs: Generated file
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.




Summary
site.py'svenv()function only triggers for a normally-launched<venv>/Scripts/python.exe), so pointingPathat a venv left both the PEP-370 user-site directory and the base install's own site-packages onsys.pathunfiltered.DLL load failed while importing win32api: The specified procedure could not be found.PythonEngine.SetNoSiteFlag()(gated on whether the venv requests--system-site-packages), withController.ClearUserSiteEnvironmentOverrideguarding against an ambientPYTHONNOUSERSITEleaking in from the host's own environment.PythonHomebeing pointed at the venv root instead of its declared base install (breaks stdlib resolution entirely), tightens venv detection against false positives, and adds a version cross-check between a venv and the Python library actually being loaded.Test plan
UiPath.Python.Tests,UiPath.Python.Activities.Tests,UiPath.Python.Activities.API.Testsall pass (85 total)--system-site-packages), base-install site-packages leak,sitecustomize.pypreservation,PythonHomeresolution, venv detection (root/Scripts/bin/false-positive), venv/library version mismatch, and ambientPYTHONNOUSERSITEoverride🤖 Generated with Claude Code