_strip_leaked_pythonpath_from_sys_path() still strips a detached venv's site-packages (the #2496 guard only covers the interpreter's own sysconfig paths)
_strip_leaked_pythonpath_from_sys_path() still strips a detached venv's site-packages — the #2496 guard only preserves the running interpreter's own sysconfig paths
Package version: 3.10.0 (latest on PyPI; also reproduced on 3.9.0) Platform: Linux x86_64 (Ubuntu 24.04.5 LTS), Python 3.14.7 Related: fixes #2484 / PR #2496 addressed a different layout — see "Why #2496 does not cover this case" below.
Summary
The import guard added in #2496 preserves PYTHONPATH entries that match the running interpreter's own sysconfig purelib/platlib. That is correct when the embedding host runs the venv's own bin/python: there sysconfig reports the venv, so the venv's site-packages survives.
It is not correct when the host runs a standalone interpreter and activates a separate dependency environment through PYTHONPATH — a "detached venv" layout (the venv's pyvenv.cfg home points at an interpreter that is not sys.executable). In that layout sysconfig.get_paths() reports the standalone interpreter's site-packages, the detached venv is never in own_site_packages, and the guard strips it — removing the directory where mempalace, chromadb, and every other dependency actually live.
The result is the same failure mode #2484 described, in a layout #2496 does not cover: the host's imports after import mempalace raise ModuleNotFoundError.
The layout that breaks
This is not exotic — it is the layout of Hermes' PM (package manager), which pins a standalone Python under ~/.hermes/tools/ and activates a managed environment directory by putting its site-packages on PYTHONPATH:
interpreter : <tools>/python-3.14.7/bin/python3 # NOT the venv's python
PYTHONPATH : <installs>/<env>/venv/lib/python3.14/site-packages
sysconfig purelib: <tools>/python-3.14.7/lib/python3.14/site-packages # ≠ the venvsys.executable -> <tools>/python-3.14.7/bin/python3
sysconfig.get_paths()["purelib"] -> <tools>/python-3.14.7/lib/python3.14/site-packages
PYTHONPATH (the managed venv) -> <installs>/<env>/venv/lib/python3.14/site-packages<installs>/<env>/venv/lib/python3.14/site-packages is not in own_site_packages, so _belongs_to_this_environment() returns False and the guard removes it.
Minimal reproduction
Generic form (works with any detached venv; <PY> is a standalone interpreter, <SP> the detached venv's site-packages):
$ PYTHONPATH=<SP> <PY> -c "
import sys
print('BEFORE:', '<SP>' in sys.path)
import mempalace
print('AFTER :', '<SP>' in sys.path)
import chromadb
"
BEFORE: True
AFTER : False
ModuleNotFoundError: No module named 'chromadb'Observed on this machine (paths shortened):
interpreter : /…/tools/python-3.14.7/bin/python3
sysconfig purelib: /…/tools/python-3.14.7/lib/python3.14/site-packages
venv on sys.path BEFORE: True
mempalace version: 3.10.0 # the wheel with the #2496 guard
mempalace from : …/mempalace/__init__.py
venv on sys.path AFTER : False
chromadb import: ModuleNotFoundError -> No module named 'chromadb'
ruamel.yaml import: ModuleNotFoundError -> No module named 'ruamel'We verified the same output with 3.9.0 and with 3.10.0 (the #2496 fix present — own_site_packages / _belongs_to_this_environment are both in the file). Upgrading to 3.10.0 does not resolve it for this layout.
Why #2496 does not cover this case
#2496's own validation note says it keeps entries matching "the running interpreter's sysconfig purelib and platlib directories", and it explicitly keeps stripping "another Python version under a shared installation prefix, a nested virtual environment". A detached venv is exactly a nested virtual environment relative to the interpreter doing the importing — so it is deliberately stripped, even though in this layout it is where mempalace itself was loaded from.
The invariant the guard needs is not "does this path belong to the running interpreter", but "could removing this path break the import that is already in progress". Deleting the directory that provided mempalace/__init__.py guarantees breakage regardless of who owns the interpreter.
Suggested fix
Preserve any PYTHONPATH entry that contains the package being imported. The minimal, layout-independent version:
# inside mempalace/__init__.py, before the strip
_pkg_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# ... and in the filter, keep entries whose realpath == _norm(_pkg_root)For a non-editable install this is the site-packages directory the package was loaded from; for an editable install it is the checkout root — both must survive. A more general option is to keep any entry from which mempalace itself resolves (importlib.util.find_spec("mempalace") at import time), since that directory is demonstrably in use by the process. Either keeps the guard's original purpose intact: paths that merely shadow a different Python's compiled extensions (and do not host this package) are still removed.
A cleaner design, if acceptable: perform the sys.path mutation only in the CLI/MCP entry points (which already drop PYTHONPATH from the environment for subprocesses), and never mutate sys.path as a side effect of being imported as a library by a host application.
Impact
Any host that embeds mempalace through a managed/detached environment layout — pinned interpreter + environment directory on PYTHONPATH — loses its dependency environment the moment it imports the package, and fails with ModuleNotFoundError on any subsequent import, including mempalace's own dependencies. In our case (Hermes on Linux, the same integration class reported in #2484) this presented as the plugin failing to load with No module named 'chromadb', plus unrelated host modules failing afterwards (ruamel.yaml, the MCP SDK).
Workaround we are using
We removed PYTHONPATH from os.environ around the mempalace imports and restored it afterwards — the strip reads os.environ["PYTHONPATH"], so this makes it a no-op — and we also run the MCP server as a separate process with PYTHONPATH unset. That is a local patch of the host plugin, not a fix; the package keeps breaking detached-venv hosts on import.
Source: MemPalace/mempalace