"cannot import name 'etree' from 'lxml'" after adding a uv venv's site-packages to a different Python's sys.path
In a persistent Python 3.14 kernel where pip install is blocked by PEP 668 (Homebrew Python, externally-managed-environment), I created a side venv with uv venv /tmp/x/.venv && uv pip install --python /tmp/x/.venv lxml and then did sys.path.insert(0, '/tmp/x/.venv/lib/python3.12/site-packages') in the kernel. The import fails with: ImportError: cannot import name 'etree' from 'lxml' (/tmp/x/.venv/lib/python3.12/site-packages/lxml/__init__.py). The lxml package directory clearly exists and lxml/__init__.py is found, so it looks like a broken or partial lxml install rather than an environment problem. Reinstalling lxml in the venv doesn't change anything.
The failure comes from a CPython version mismatch, not from lxml. uv venv with no --python picked a different interpreter (3.12) than the kernel (3.14). lxml/__init__.py is pure Python and imports on any version. lxml.etree is a compiled extension named etree.cpython-312-darwin.so, and a 3.14 interpreter only looks for *.cpython-314-* (or abi3) suffixes. The extension is skipped without any message, and you get cannot import name 'etree' instead of an ABI or version error.
Fix: build the venv for the same interpreter as the consumer, and purge the half-imported package before retrying:
uv venv --python 3.14 /tmp/x/.venv # match sys.version of the kernel
uv pip install --python /tmp/x/.venv lxmlimport sys
sys.path[:] = [p for p in sys.path if 'python3.12' not in p]
for m in [k for k in sys.modules if k.startswith('lxml')]: del sys.modules[m]
sys.path.insert(0, '/tmp/x/.venv/lib/python3.14/site-packages')
from lxml import etreeThis applies to any package that has a pure-Python __init__ and compiled submodules (numpy, pydantic-core, and others): the top-level import succeeds and the first compiled submodule fails with a misleading cannot import name or No module named error. Check sys.version against the lib/pythonX.Y directory before suspecting the package.