pip安装私有Python包时报'-e git+h'解析错误该如何解决?
报错原因
- setuptools 的
install_requires字段仅支持符合 PEP 508 规范的依赖声明,不识别-e开头的可编辑安装标识。之前可正常运行是因为旧版本 setuptools 对依赖格式校验宽松,升级到高版本后校验规则收紧,直接触发解析错误。 - 你当前的代码直接将 requirements.txt 全量内容读取到
install_requires中,包含不符合规范的-e git+xxx格式行,所以解析失败。
解决方案
方案1:修改依赖声明为PEP 508兼容格式(最推荐)
直接修改 requirements.txt 中的 zempy 依赖行,去掉 -e 标识,用 @ 关联 Git 地址即可,无需修改 setup.py 代码:
zempy @ git+https://gitlab.com/group/zempy.git@0.4.7
修改后直接执行安装命令即可正常拉取对应版本的私有依赖。
方案2:保留可编辑安装需求的处理方式
如果需要保留 zempy 的可编辑安装模式,按以下步骤操作:
- 保留 requirements.txt 中的
-e git+https://gitlab.com/group/zempy.git@0.4.7#egg=zempy行 - 修改 setup.py 中的
get_requirements函数,过滤掉非法格式的行:
def get_requirements(): """ Return requirements as list. """ with open('requirements.txt') as f: req_list = [] for line in f.readlines(): line = line.strip() # 过滤空行、注释行、-e开头的可编辑依赖行 if not line or line.startswith("#") or line.startswith("-e"): continue req_list.append(line) return req_list
- 安装 hidden 包前,先单独安装 zempy:
python3 -m pip install -e git+https://gitlab.com/group/zempy.git@0.4.7#egg=zempy
- 再执行
python3 -m pip install .或python3 -m pip install -e .安装 hidden 包即可。
内容的提问来源于stack exchange,提问作者emremrah
相关产品推荐
相关产品推荐

