Python 如何在 requirements.txt 中声明一个直接的 github 源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/16584552/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to state in requirements.txt a direct github source
提问by Alfe
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.  This works fine and I want to have that dependency in my requirements.txt.  I've looked at other tickets like thisbut that didn't solve my problem.  If I put something like
它直接从 Github 存储库安装它。这很好用,我想在我的requirements.txt. 我看其他的票像这样但这并没有解决我的问题。如果我把类似的东西
-f git+git://github.com/mozilla/elasticutils.git
elasticutils==0.7.dev
in the requirements.txtfile, a pip install -r requirements.txtresults in the following output:
在requirements.txt文件中,apip 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 filedoes not mention links using the git+gitprotocol specifier, so maybe this is just not supported.
需求文件的文档没有提到使用git+git协议说明符的链接,所以可能只是不支持。
Does anybody have a solution for my problem?
有人对我的问题有解决方案吗?
采纳答案by stalk
“Editable” packages syntaxcan be used in requirements.txtto 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
回答by osa
First, install with git+gitor git+https, in any way you know. Example of installing kronok's branch of the brabeionproject:
首先,以您知道的任何方式使用git+git或 进行安装git+https。安装项目kronok的分支示例brabeion:
pip install -e git+https://github.com/kronok/brabeion.git@12efe6aa06b85ae5ff725d3033e38f624e0a616f#egg=brabeion
Second, use pip freeze > requirements.txtto get the right thing in your requirements.txt. In this case, you will get
其次,使用pip freeze > requirements.txt在您的requirements.txt. 在这种情况下,您将获得
-e git+https://github.com/kronok/brabeion.git@12efe6aa06b85ae5ff725d3033e38f624e0a616f#egg=brabeion-master
Third, test the result:
三、测试结果:
pip uninstall brabeion
pip install -r requirements.txt
回答by TrinitronX
Since pip v1.5, (released Jan 1 2014: CHANGELOG, PR) you may also specify a subdirectory of a git repo to contain your module.  The syntax looks like this:
由于 pip v1.5,(2014 年 1 月 1 日发布:CHANGELOG, PR)您还可以指定 git repo 的子目录来包含您的模块。语法如下所示:
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. Yet this feature is helpful for some pre-existing repos that contain python modules in subdirectories. You might be forced to install them this way if they are not published to pypi too.
注意:作为 pip 模块作者,理想情况下,如果可以,您可能希望将模块发布到它自己的顶级存储库中。然而,此功能对于某些在子目录中包含 python 模块的预先存在的存储库很有帮助。如果它们也没有发布到 pypi,您可能会被迫以这种方式安装它们。
回答by qff
requirements.txtallows the following ways of specifying a dependency on a package in a git repository as of pip 7.0:1
requirements.txt从 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 [email protected]: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 -eflag in the other answers so here's my clarification:
为什么是额外的答案?
我-e对其他答案中的标志有些困惑,所以这是我的澄清:
The -eor --editableflag means that the package is installed in <venv path>/src/SomeProjectand thus not in the deeply buried <venv path>/lib/pythonX.X/site-packages/SomeProjectit would otherwise be placed in.2
的-e或--editable标记的装置,该包装被安装在<venv path>/src/SomeProject并因此没有在深埋<venv path>/lib/pythonX.X/site-packages/SomeProject它否则将被放置英寸2
Documentation
文档
回答by YPCrumble
Normally your requirements.txtfile 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-twousing a GitHub repo. The text between @and #denotes the specifics of the package.
以下示例package-two使用 GitHub 存储库进行更新。@和之间的文字#表示包装的细节。
Specify commit hash (41b95ecin the context of updated requirements.txt):
指定提交哈希(41b95ec在 updated 上下文中requirements.txt):
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/[email protected]#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-twois not a comment here, it is to explicitly state the package name
注意#egg=package-two这里不是注释,是明确说明包名
This blog posthas some more discussion on the topic.
这篇博文对这个话题有更多的讨论。
回答by Throw Away Account
I'm finding that it's kind of tricky to get pip3 (v9.0.1, as installed by Ubuntu 18.04's package manager) to actually install the thing I tell it to install. I'm posting this answer to save anyone's time who runs into this problem.
我发现让 pip3(v9.0.1,由 Ubuntu 18.04 的包管理器安装)来实际安装我告诉它安装的东西有点棘手。我发布此答案是为了节省遇到此问题的任何人的时间。
Putting this into a requirements.txt file failed:
将其放入 requirements.txt 文件失败:
git+git://github.com/myname/myrepo.git@my-branch#egg=eggname
By "failed" I mean that while it downloaded the code from Git, it ended up installing the original version of the code, as found on PyPi, instead of the code in the repo on that branch.
“失败”是指当它从 Git 下载代码时,它最终安装了原始版本的代码,如在 PyPi 上找到的那样,而不是该分支上的 repo 中的代码。
However, installing the commmit instead of the branch name works:
但是,安装提交而不是分支名称有效:
git+git://github.com/myname/myrepo.git@d27d07c9e862feb939e56d0df19d5733ea7b4f4d#egg=eggname

