fix(cookbook): recognize runtime --user installs by replaying user-site .pth hooks#4811
Open
devpedrobarbosa wants to merge 2 commits into
Conversation
Local optional-dep probing in `list_packages` decides "installed" by importing the package in-process. When a dep is installed at runtime via Cookbook -> Dependencies (`pip install --user`) into a long-lived server that started before the install, user-site was never processed by `site`. The recovery used `sys.path.append(user_site)`, which makes modules importable but does NOT execute user-site `.pth` files. On Python 3.12+ `distutils` is gone from the stdlib and is only restored by setuptools' `distutils-precedence.pth` (shipped in user-site). basicsr (a realesrgan dep) does `import distutils` at import time, so the probe failed with `ModuleNotFoundError: No module named 'distutils'` and reported realesrgan as not installed until a full restart. Use `site.addsitedir(user_site)` instead so user-site `.pth` hooks are replayed and the distutils shim activates. Verified end-to-end on the python:3.14 image: realesrgan now probes as installed without a restart. Fixes pewdiepie-archdaemon#4810 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The regression test pinned the old `sys.path.append(user_site)` source text. Update it to assert the new `site.addsitedir(user_site)` guarantee that user-site `.pth` hooks are replayed for runtime --user installs. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #4694. The Real-ESRGAN build now succeeds, but the dependency was still shown as not installed in Cookbook → Dependencies, and a browser hard refresh never fixed it — only a full container restart did.
Root cause: the local optional-dep probe in
list_packagesdecides "installed" by importing the package in-process. Cookbook installs optional deps withpip install --user, so the package lands in user-site along with setuptools'distutils-precedence.pth. On Python 3.12+distutilsis gone from the stdlib and is only restored by that.pth. When the dep is installed into an already-running server, user-site was never processed bysite; the recovery usedsys.path.append(user_site), which makes modules importable but does not execute user-site.pthfiles. Soimport basicsr(a realesrgan dep) →import distutilsraisedModuleNotFoundError: No module named 'distutils', the probe caught it asImportError, and realesrgan read as not-installed until a process restart.Fix: use
site.addsitedir(user_site)instead ofsys.path.append(user_site)so user-site.pthhooks (incl. the setuptools distutils shim) are replayed when a package is installed into a running process.Target branch
dev, notmain.Linked Issue
Fixes #4810
Type of Change
Checklist
devdocker compose up) and verified the change works end-to-end. Type-checks and unit tests are not enough.How to Test
On the
python:3.14image (docker compose up -d --build):realesrganfrom Cookbook → Dependencies. It builds/installs (importlib.metadata.version("realesrgan") == "0.3.0"), but the panel still shows it not installed, and a hard refresh doesn't help. Confirmed the in-process probe path fails:site.addsitedir(...)and resolves:.pthis user-site only:distutils-precedence.pthexists in/app/.local/lib/python3.14/site-packages/but not in the main site-packages — which is why a plainsys.path.appendleft distutils unavailable.Visual / UI changes — REQUIRED if you touched anything that renders
N/A — no UI/rendering changes. Touches only
routes/shell_routes.py(one line in thelist_packagesuser-site prologue, plus an explanatory comment).