pip怎样通过git来安装包

打印 上一主题 下一主题

主题 1802|帖子 1802|积分 5408

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
pip 支持通过 git 安装 Python 包,通常用于安装托管在 Git 堆栈中的自界说包或者未发布到 PyPI 的包。以下是通过 git 安装 Python 包的几种方法和本事。

1. 根本语法

通过 git 安装包的根本命令格式如下:
  1. pip install git+<repository_url>
复制代码
例子

安装托管在 GitHub 上的一个包:
  1. pip install git+https://github.com/username/repository.git
复制代码

2. 指定分支

如果堆栈有多个分支,可以通过 @branch_name 指定要安装的分支:
  1. pip install git+https://github.com/username/repository.git
  2. @branch_name
复制代码
例子

安装堆栈中 develop 分支的代码:
  1. pip install git+https://github.com/username/repository.git
  2. @develop
复制代码

3. 指定提交哈希(commit hash)

如果必要安装特定版本的代码,可以通过 @commit_hash 指定提交哈希:
  1. pip install git+https://github.com/username/repository.git
  2. @commit_hash
复制代码
例子

安装某个特定提交(比方,abc123):
  1. pip install git+https://github.com/username/repository.git
  2. @abc123
复制代码

4. 指定标签(Tag)

通过 Git 标签安装特定版本的包:
  1. pip install git+https://github.com/username/repository.git
  2. @tag_name
复制代码
例子

安装某个版本的包(比方,v1.0.0):
  1. pip install git+https://github.com/username/repository.git
  2. @v1.0.0
复制代码

5. 包含子目录的包

如果包代码不在堆栈根目录下,而在子目录中,可以通过 #subdirectory 指定子目录路径:
  1. pip install git+https://github.com/username/repository.git
  2. @branch_name#subdirectory=path/to/subdirectory
复制代码
例子

堆栈结构如下:
  1. repository/
  2. ├── submodule/
  3. │   ├── setup.py
复制代码
安装 submodule 子目录中的包:
  1. pip install git+https://github.com/username/repository.git
  2. @main#subdirectory=submodule
复制代码

6. 利用 SSH 安装

如果堆栈是私有的,且你有 SSH 访问权限,可以通过 SSH 克隆并安装:
  1. pip install git+ssh://git@github.com/username/repository.git
复制代码
例子

  1. pip install git+ssh://git@github.com/username/repository.git
  2. @main
复制代码

7. 安装到本地(开辟模式)

如果你渴望以开辟模式安装包,可以加上 -e 参数(editable mode)。这适用于本地堆栈或长途堆栈:
  1. pip install -e git+<repository_url>
复制代码
例子

  1. pip install -e git+https://github.com/username/repository.git@develop
复制代码

8. 指定 requirements.txt 文件中的 Git 包

在 requirements.txt 中,也可以指定通过 git 安装的包:
  1. git+https://github.com/username/repository.git@main
  2. git+https://github.com/username/repository.git@v1.0.0#subdirectory=path/to/submodule
复制代码
然后用以下命令安装:
  1. pip install -r requirements.txt
复制代码

9. 利用访问令牌(私有堆栈)

如果要安装私有堆栈中的包,且没有利用 SSH,可以利用 GitHub 的访问令牌(Personal Access Token, PAT):
  1. pip install git+https://<token>@github.com/username/repository.git
复制代码
例子

假设访问令牌是 ghp_ABC1234567890:
  1. pip install git+https://ghp_ABC1234567890@github.com/username/repository.git
复制代码

10. 留意事项


  • 安装带有依赖的包: 确保堆栈中的 setup.py 或 pyproject.toml 文件已正确配置依赖项。
  • 版本控制: 如果没有指定分支、标签或提交哈希,pip 默认会安装堆栈的 HEAD。
  • 私有堆栈访问: 利用 SSH 或访问令牌时,请留意敏感信息的安全性。
通过这些方法,你可以轻松通过 git 安装托管在长途或本地的 Python 包。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81428

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表