python module_error ai_generated true

ModuleNotFoundError:没有名为 'mypkg' 的模块 # 在 pip install -e . 成功后

ModuleNotFoundError: No module named 'mypkg' # after pip install -e . succeeded

ID: python/pip-editable-install-missing-pth

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-04-30首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

可编辑安装创建了一个 __editable__*.pth 文件,指向不再存在的路径(项目被移动、包目录被重命名),或因为 site-packages 不在 sys.path 上而未加载该 .pth。

English

Editable install created a __editable__*.pth file pointing at a path that no longer exists (moved project, renamed package dir), or the .pth was not picked up because site-packages is not on sys.path.

generic

解决方案

  1. 95% 成功率
    python -m pip uninstall -y mypkg
    cd /current/path/to/project
    python -m pip install -e .
  2. 88% 成功率
    python - <<'PY'
    import site, pathlib
    for sp in site.getsitepackages():
        for pth in pathlib.Path(sp).glob('__editable__*mypkg*.pth'):
            print(pth, '->', pth.read_text().strip())
    PY
    # then remove stale entries and reinstall

无效尝试

常见但无效的做法:

  1. 70% 失败

    If the project was moved, pip regenerates the same stale path or a duplicate .pth.

  2. 75% 失败

    Hardcodes a machine-specific path and does not fix the broken editable metadata.