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

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

我已经使用以下命令安装了一个库

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

直接从Github存储库安装它。这工作正常,我想在我的requirements.txt。我看其他的票像这样但这并没有解决我的问题。如果我把像

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

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))

需求文件文档中没有提及使用git+git协议说明符的链接,因此也许只是不被支持。

有人能解决我的问题吗?

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 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:

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.

Does anybody have a solution for my problem?


回答 0

“ Editable”包语法可用于requirements.txt从各种VCS(git,hg,bzr,svn)导入包:

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

另外,可以指向特定的提交:

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

“Editable” packages syntax can be used in requirements.txt to import packages from a variety of 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

回答 1

通常,您的requirements.txt文件如下所示:

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

要指定Github存储库,您不需要package-name==约定。

下面的示例更新 package-two使用GitHub存储库进行。@和之间的文字#表示包装的详细信息。

指定提交哈希(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

指定分支名称(master):

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

指定标签(0.1):

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

指定发布(3.7.1):

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

请注意,#egg=package-two此处不是注释,而是要明确说明软件包名称

这篇博客文章对此主题进行了更多讨论。

Normally your requirements.txt file would look something like this:

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.

The examples below update package-two using a GitHub repo. The text between @ and # denotes the specifics of the package.

Specify commit hash (41b95ec in the context of 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):

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

Specify tag (0.1):

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

Specify release (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

This blog post has some more discussion on the topic.


回答 2

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 git+git@git.myproject.org:SomeProject#egg=SomeProject

对于Github,这意味着您可以做到(请注意,省略了-e):

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

为什么要额外回答?
-e在其他答案中对标志有些困惑,所以这是我的澄清:

-e或” --editable标志表示包装已安装在<venv path>/src/SomeProject深处,因此不会放入深处<venv path>/lib/pythonX.X/site-packages/SomeProject。否则,它将被放置在其中。2

文献资料

requirements.txt allows the following ways of specifying a dependency on a package in a git repository as of pip 7.0: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):

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:

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

Documentation


回答 3

首先,以任何已知的方式使用git+git或安装git+https。安装项目kronok的分支的示例brabeion

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

其次,使用pip freeze > requirements.txt来获取正确的东西requirements.txt。在这种情况下,您将获得

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

三,测试结果:

pip uninstall brabeion
pip install -r requirements.txt

First, install with git+git or git+https, in any way you know. Example of installing kronok‘s branch of the brabeion project:

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. 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

由于pip v1.5(发布于2014年1月1日:CHANGELOGPR),您还可以指定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

注意:作为pip模块的作者,如果可能的话,理想情况下,您可能希望将模块发布到它自己的顶级仓库中。但是,此功能对于某些子目录中包含python模块的现有存储库很有帮助。如果它们也没有发布到pypi,则可能会被迫以这种方式安装它们。

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 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.


回答 5

我发现要获取pip3(v9.0.1,由Ubuntu 18.04的软件包管理器安装)来实际安装我告诉它要安装的东西有点棘手。我发布此答案是为了节省遇到此问题的任何人的时间。

将其放入Requirements.txt文件失败:

git+git://github.com/myname/myrepo.git@my-branch#egg=eggname

“失败”是指当它从Git下载代码时,它最终安装了PyPi上找到的代码的原始版本,而不是该分支上存储库中的代码。

但是,安装commmit而不是分支名称是可行的:

git+git://github.com/myname/myrepo.git@d27d07c9e862feb939e56d0df19d5733ea7b4f4d#egg=eggname

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.

Putting this into a requirements.txt file failed:

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.

However, installing the commmit instead of the branch name works:

git+git://github.com/myname/myrepo.git@d27d07c9e862feb939e56d0df19d5733ea7b4f4d#egg=eggname