如何使用Pipenv将私有Git仓库中的工具包导入项目
使用Pipenv安装带setup.py的私有工具包失败排查
问题背景
我有一个带setup.py的通用工具仓库,想通过Pipenv把它安装到新项目里,但一直报错。平时很少接触setup.py,搞不清哪里操作错了,需要帮忙找出问题。
通用工具仓库详情
目录结构
. ├── setup.py └── src └── utils ├── __init__.py └── utils.py.py
setup.py代码
#!/usr/bin/env python # -*- coding: utf-8 -* from setuptools import find_packages, setup install_requires = (...) # 这里是具体依赖包列表 setup( name="common_utils", version="0.0.1", include_package_data=True, package_dir={"": "src"}, packages=find_packages("src", include=["utils"]), install_requires=install_requires, process_dependency_links=True, )
新项目配置
新项目的Pipfile里是这么引用工具包的:
... [ packages ] common_utils = { git = "git@github.com:me/utils-repo" }
报错内容
执行pipenv install时弹出错误:
pipenv.patched.pip._internal.exceptions.InstallationError: Invalid requirement: 'common_utils@ git+git@github.com:me/utils-repo.git' (from line 2 of ...) Hint: It looks like a path. File 'common_utils@ git+git@github.com:me/utils-repo.git' does not exist.
问题排查与解决方法
1. Pipfile引用格式错误
这是当前报错的直接原因,Pipenv解析Git仓库引用时格式不对。修改Pipfile的写法,补充Git URL的完整后缀和分支/标签指定:
[ packages ] common_utils = { git = "git@github.com:me/utils-repo.git", ref = "main" }
注:ref的值换成你实际用的分支名,比如master、dev,或者具体的版本标签、提交哈希
2. 工具仓库的setup.py与文件问题
虽然当前报错不是这个导致的,但提前修复避免后续问题:
utils.py.py文件名明显有误,改成utils.py,不然以后导入模块会出问题find_packages("src", include=["utils"])可以简化成find_packages(where="src"),因为已经通过package_dir={"": "src"}指定了源码路径process_dependency_links=True在新版setuptools里已经被废弃,如果你的依赖没有特殊的外部链接需求,直接删掉这个参数
3. 私有仓库权限验证
确保本地已经配置了能访问该GitHub私有仓库的SSH密钥,否则Pipenv拉取不到代码。先手动执行git clone git@github.com:me/utils-repo.git测试是否能正常克隆。
验证步骤
- 按上面的方法修改新项目的Pipfile
- 修复工具仓库里的文件名错误
- 执行
pipenv install --skip-lock先跳过锁文件生成,测试安装是否成功 - 安装成功后再运行
pipenv lock生成锁文件
内容的提问来源于stack exchange,提问作者WhatAmIDoing
相关产品推荐
相关产品推荐

