如何在Requirements.txt中声明直接的github源

本文介绍了如何在requirements.txt文件中直接引用GitHub仓库作为Python依赖。内容包括使用指定的提交哈希、分支名、标签或发行版进行安装,并解释了相关pip命令的语法和用途。还讨论了自pip 7.0以来支持的子目录指定功能。
摘要由CSDN通过智能技术生成

本文翻译自:How to state in requirements.txt a direct github source

I've installed a library using the command 我已经使用以下命令安装了一个库

pip install git+git://github.com/mozilla/elasticutils.git

which installs it directly from a Github repository. 直接从Github存储库安装它。 This works fine and I want to have that dependency in my requirements.txt . 这工作正常,我想在我的requirements.txt具有该依赖关系。 I've looked at other tickets like this but that didn't solve my problem. 我看其他的票像这样但这并没有解决我的问题。 If I put something like 如果我把像

-f git+git://github.com/mozilla/elasticutils.git
elasticutils==0.7.dev

in the requirements.txt file, a pip install -r requirements.txt results in the following output: requirements.txt文件中, pip install -r requirements.txt导致以下输出:

Downloading/unpacking elasticutils==0.7.dev (from -r requirements.txt (line 20))
  Could not find a version that satisfies the requirement elasticutils==0.7.dev (from -r requirements.txt (line 20)) (from versions: )
No distributions matching the version for elasticutils==0.7.dev (from -r requirements.txt (line 20))

The documentation of the requirements file does not mention links using the git+git protocol specifier, so maybe this is just not supported. 需求文件文档没有提及使用git+git协议说明符的链接,因此也许只是不支持。

Does anybody have a solution for my problem? 有人能解决我的问题吗?


#1楼

参考:https://stackoom.com/question/17aOm/如何在Requirements-txt中声明直接的github源


#2楼

“Editable” packages syntax can be used in requirements.txt to import packages from a variety of VCS (git, hg, bzr, svn) : “可编辑”包语法可被用requirements.txt到从各种导入包VCS(GIT,HG,BZR,SVN) :

-e git://github.com/mozilla/elasticutils.git#egg=elasticutils

Also, it is possible to point to particular commit: 另外,可以指向特定的提交:

-e git://github.com/mozilla/elasticutils.git@000b14389171a9f0d7d713466b32bc649b0bed8e#egg=elasticutils

#3楼

First, install with git+git or git+https , in any way you know. 首先,以您知道的任何方式使用git+gitgit+https安装。 Example of installing kronok 's branch of the brabeion project: 安装的例子kronok的的的分支brabeion项目:

pip install -e git+https://github.com/kronok/brabeion.git@12efe6aa06b85ae5ff725d3033e38f624e0a616f#egg=brabeion

Second, use pip freeze > requirements.txt to get the right thing in your requirements.txt . 其次,使用pip freeze > requirements.txt在您的requirements.txt获得正确的内容。 In this case, you will get 在这种情况下,您将获得

-e git+https://github.com/kronok/brabeion.git@12efe6aa06b85ae5ff725d3033e38f624e0a616f#egg=brabeion-master

Third, test the result: 三,测试结果:

pip uninstall brabeion
pip install -r requirements.txt

#4楼

Since pip v1.5 , (released Jan 1 2014: CHANGELOG , PR ) you may also specify a subdirectory of a git repo to contain your module. 从pip v1.5 (2014年1月1日发布: CHANGELOGPR ),您还可以指定git repo的子目录来包含您的模块。 The syntax looks like this: 语法如下所示:

pip install -e git+https://git.repo/some_repo.git#egg=my_subdir_pkg&subdirectory=my_subdir_pkg # install a python package from a repo subdirectory

Note: As a pip module author, ideally you'd probably want to publish your module in it's own top-level repo if you can. 注意:作为pip模块的作者,如果可能的话,理想情况下,您可能希望将模块发布到它自己的顶级仓库中。 Yet this feature is helpful for some pre-existing repos that contain python modules in subdirectories. 但是,此功能对于某些子目录中包含python模块的现有存储库很有帮助。 You might be forced to install them this way if they are not published to pypi too. 如果它们也没有发布到pypi,则可能会被迫以这种方式安装它们。


#5楼

requirements.txt allows the following ways of specifying a dependency on a package in a git repository as of pip 7.0: 1 requirements.txt允许通过以下方式在git pip 7.0及更高版本中指定对git存储库中软件包的依赖关系: 1

[-e] git+git://git.myproject.org/SomeProject#egg=SomeProject
[-e] git+https://git.myproject.org/SomeProject#egg=SomeProject
[-e] git+ssh://git.myproject.org/SomeProject#egg=SomeProject
-e git+git@git.myproject.org:SomeProject#egg=SomeProject

For Github that means you can do (notice the omitted -e ): 对于Github,这意味着您可以执行此操作(注意省略的-e ):

git+git://github.com/mozilla/elasticutils.git#egg=elasticutils

Why the extra answer? 为什么要额外回答?
I got somewhat confused by the -e flag in the other answers so here's my clarification: 在其他答案中,我对-e标志有些困惑,所以这是我的澄清:

The -e or --editable flag means that the package is installed in <venv path>/src/SomeProject and thus not in the deeply buried <venv path>/lib/pythonX.X/site-packages/SomeProject it would otherwise be placed in. 2 -e--editable标志意味着该软件包安装在<venv path>/src/SomeProject ,因此不会安装在深埋的<venv path>/lib/pythonX.X/site-packages/SomeProject中。放入。2

Documentation 文献资料


#6楼

Normally your requirements.txt file would look something like this: 通常,您的requirements.txt文件如下所示:

package-one==1.9.4
package-two==3.7.1
package-three==1.0.1
...

To specify a Github repo, you do not need the package-name== convention. 要指定Github存储库,您不需要package-name==约定。

The examples below update package-two using a GitHub repo. 以下示例使用GitHub存储库更新了第二package-two The text between @ and # denotes the specifics of the package. @#之间的文本表示软件包的详细信息。

Specify commit hash ( 41b95ec in the context of updated requirements.txt ): 指定提交哈希(在更新的requirements.txt上下文中为41b95ec ):

package-one==1.9.4
git+git://github.com/path/to/package-two@41b95ec#egg=package-two
package-three==1.0.1

Specify branch name ( master ): 指定分支名称( master ):

git+git://github.com/path/to/package-two@master#egg=package-two

Specify tag ( 0.1 ): 指定标签( 0.1 ):

git+git://github.com/path/to/package-two@0.1#egg=package-two

Specify release ( 3.7.1 ): 指定发行版( 3.7.1 ):

git+git://github.com/path/to/package-two@releases/tag/v3.7.1#egg=package-two

Note that #egg=package-two is not a comment here, it is to explicitly state the package name 请注意, #egg=package-two在这里不是注释,它是明确声明软件包名称

This blog post has some more discussion on the topic. 这篇博客文章对此主题进行了更多讨论。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值