Python Editable Install三层导入隔离模式(Python-Editable-Import-Isolation)#

背景与动机#

现代Python构建后端(scikit-build-core、setuptools、hatchling、flit)的editable install(pip install -e .)不再是简单的sys.path插入,而是通过在sys.meta_path上安装自定义MetaPathFinder来实现"无需复制文件即可从源码目录导入"。这些finder(如ScikitBuildRedirectingFinder、_EditableFinder、EditableProjectFinder)在.pth文件处理时被激活,运行在sys.path搜索之前,能够绕过sys.path直接将import重定向到真实源码目录。

这对测试"原生库缺失"、"包未安装"等隔离场景造成隐形障碍:仅操作sys.path(移除源码目录、插入临时目录)无法阻止editable finder将import重定向回真实源码,导致:

  • 测试"无原生库时优雅降级"时,finder找到真实.so文件,is_available()返回True(假阳性)

  • 测试"包未安装时报错"时,finder静默重定向,import不会失败

  • 测试隔离环境被"看不见的手"污染,问题难以复现和诊断

传统隔离方案(temporary-syspath-modification)只处理sys.path一层,在现代Python打包生态中已不足够。本模式提供三层清理策略,确保完全隔离。


触发场景#

  • 测试原生扩展缺失时的Python-only降级/优雅回退行为

  • 测试包未正确安装时的错误提示

  • CI中验证wheel安装而非editable install的行为

  • 创建临时/最小化Python环境进行集成测试

  • 需要在子进程中"假装某个包没有C扩展"的场景

不适用于:

  • 纯Python无C扩展项目的简单单元测试(用pytest的monkeypatch足够)

  • 测试已安装wheel包的正常功能(不需要隔离editable finder)


核心步骤#

三层必须按顺序清理,且必须在第一次import目标包之前完成:

第一层:清理 sys.meta_path(Meta Path Finders)#

这是最关键的一层,也是最容易遗漏的层。editable install的finder挂在sys.meta_path上,在sys.path搜索之前拦截import。

# 在子进程的最开头,import目标包之前执行
import sys

# 移除所有editable相关finder
_finders_to_remove = []
for i, finder in enumerate(sys.meta_path):
    finder_name = type(finder).__name__
    finder_module = type(finder).__module__
    # 识别常见editable finder(名称/模块包含'editable'或'redirecting'关键词)
    if 'editable' in finder_name.lower() or 'redirecting' in finder_name.lower() \
       or 'editable' in finder_module.lower() or 'redirecting' in finder_module.lower():
        _finders_to_remove.append(i)
# 倒序删除避免索引偏移
for i in reversed(_finders_to_remove):
    sys.meta_path.pop(i)

注意:不同构建后端的finder名称不同,需要根据实际后端匹配:

构建后端

Finder类名/模块

scikit-build-core

_editable_skbc_* / ScikitBuildRedirectingFinder

setuptools

_EditableFinder / _EditableNamespaceFinder

hatchling

EditableProjectFinder

flit

通常用__editable__包重定向

通用策略是匹配finder的类名/模块名是否包含'editable'或'redirecting'关键词。

第二层:清理 sys.path(搜索路径)#

移除真实源码目录,插入临时包目录:

import os
import tempfile

# 临时包目录(不含C扩展.so/.pyd/.dll)
_temp_dir = tempfile.mkdtemp()
# ... 复制纯Python文件到_temp_dir/caffe_ffi/(排除*.so,*.pyd,*.dll,__pycache__)

# 从sys.path移除真实源码路径
_real_source = os.path.join(os.path.dirname(__file__), '..', '..')  # 项目根
_real_source = os.path.realpath(_real_source)
sys.path[:] = [p for p in sys.path if os.path.realpath(p) != _real_source]
# 同时移除site-packages中的editable .pth目录和源码dir
for p in list(sys.path):
    if 'caffe-ffi' in p and 'site-packages' in p:
        sys.path.remove(p)

# 将临时目录插入最前端
sys.path.insert(0, _temp_dir)

第三层:清理 sys.modules(模块缓存)#

如果父进程已经import过目标包(或conftest.py触发了import),sys.modules中会有缓存,必须清除:

# 清除所有caffe_ffi相关的已缓存模块
_mods_to_remove = [k for k in sys.modules if k == 'caffe_ffi' or k.startswith('caffe_ffi.')]
for k in _mods_to_remove:
    del sys.modules[k]

推荐执行方式:subprocess隔离#

父进程中直接修改sys.meta_path/sys.path/sys.modules会影响父进程状态,且父进程可能已初始化过一些全局变量。最佳实践是使用subprocess启动一个干净的Python子进程,在子进程中执行三层清理:

import subprocess
import sys
import textwrap

def test_python_only_fallback_when_native_lib_missing():
    """Verify Python-only mode works when native library is missing."""
    # 子进程代码:三层清理 + import + 断言
    child_code = textwrap.dedent("""
        import sys, os, shutil, tempfile

        # === 第一层:清理sys.meta_path ===
        _finders_to_remove = []
        for i, finder in enumerate(sys.meta_path):
            fn = type(finder).__name__.lower()
            fm = type(finder).__module__.lower()
            if 'editable' in fn or 'redirecting' in fn or 'editable' in fm or 'redirecting' in fm:
                _finders_to_remove.append(i)
        for i in reversed(_finders_to_remove):
            sys.meta_path.pop(i)

        # === 创建临时包目录(不含.so/.pyd/.dll) ===
        _src = os.path.join(os.path.dirname(__file__), 'caffe_ffi')
        _tmp = tempfile.mkdtemp()
        _dst = os.path.join(_tmp, 'caffe_ffi')
        shutil.copytree(_src, _dst,
            ignore=shutil.ignore_patterns('*.so', '*.pyd', '*.dll', '*.pyc', '__pycache__'))

        # === 第二层:清理sys.path ===
        _real = os.path.realpath(os.path.join(_src, '..', '..'))
        sys.path[:] = [p for p in sys.path if os.path.realpath(p) != _real]
        sys.path.insert(0, _tmp)

        # === 第三层:清理sys.modules ===
        for k in [k for k in sys.modules if k == 'caffe_ffi' or k.startswith('caffe_ffi.')]:
            del sys.modules[k]

        # === 验证 ===
        import caffe_ffi
        assert not caffe_ffi.is_available(), "FFI should NOT be available without .so"
        assert caffe_ffi.lib_path() is None, "lib_path should be None"
        b = caffe_ffi.Blob([2, 3])
        b.fill(1.0)
        assert b.data_tensor.shape == (2, 3)
        print("REGRESSION_OK")
    """)

    result = subprocess.run(
        [sys.executable, '-c', child_code],
        capture_output=True, text=True, timeout=30,
        cwd=os.path.dirname(__file__),
        env={**os.environ, "PYTHONDONTWRITEBYTECODE": "1"},
    )
    assert result.returncode == 0, f"Child failed: {result.stderr}"
    assert "REGRESSION_OK" in result.stdout, f"Unexpected output: {result.stdout}"

反模式与陷阱#

陷阱

后果

正确做法

只清sys.path不清meta_path

editable finder绕过sys.path直接重定向,隔离失效

三层必须全部清理,meta_path最先清

父进程内直接修改不使用subprocess

修改影响父进程其他测试,且无法验证import时行为

用subprocess创建隔离进程

忘记清sys.modules缓存

父进程已import的模块直接返回缓存对象,不触发finder/path搜索

import前清除目标包所有子模块缓存

copytree时不排除*.so/*.pyd

临时目录中仍有原生库,测试前提被破坏

ignore_patterns排除所有原生扩展文件

只匹配特定finder名称

不同构建后端/版本finder名称不同,漏匹配导致隔离失效

关键词匹配(editable/redirecting)而非精确名称

清理顺序错误(先import再清理)

import时finder已执行,模块已缓存,清理无效果

三层清理必须在第一次import目标包之前完成

用os.chdir而非修改sys.path

cwd不影响editable finder(finder用绝对路径重定向)

显式修改sys.path和meta_path


验证检查清单#

  • [ ] 子进程中is_available() == False(确认C扩展未被加载)

  • [ ] sys.meta_path中不含名称含'editable'/'redirecting'的finder

  • [ ] sys.path中不含项目真实源码目录

  • [ ] sys.modules中不含目标包缓存(import前检查)

  • [ ] 临时包目录中没有*.so/.pyd/.dll文件

  • [ ] 子进程退出码为0,断言通过

  • [ ] 打印唯一成功标记(如"REGRESSION_OK")避免stdout误判


参考实现#