Python 如何让 setuptools 安装不在 PyPI 上的软件包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3472430/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:14:42  来源:igfitidea点击:

How can I make setuptools install a package that's not on PyPI?

pythonsetuptoolsdistutilspypi

提问by andrei

I've just started working with setuptools and virtualenv. My package requires the latest python-gearman that is only available from GitHub. The python-gearman version that's on PyPI is an old one. The Github source is setuptools-compatible, i.e. has setup.py, etc. Is there a way to make setuptools download and install the new version instead of looking for it on PyPI and installing the old one?

我刚刚开始使用 setuptools 和 virtualenv。我的包需要最新的 python-gearman,它只能从 GitHub 获得。PyPI 上的 python-gearman 版本是旧版本。Github 源是 setuptools 兼容的,即有 setup.py 等。有没有办法让 setuptools 下载并安装新版本,而不是在 PyPI 上查找并安装旧版本?

FYI, the new python-gearman is http://github.com/mtai/python-gearman

仅供参考,新的 python-gearman 是http://github.com/mtai/python-gearman

采纳答案by PJ Eby

The key is to tell easy_install where the package can be downloaded. In this particular case, it can be found at the url http://github.com/mtai/python-gearman/tarball/master. However, that link by itself won't work, because easy_install can't tell just by looking at the URL what it's going to get.

关键是要告诉easy_install在哪里可以下载包。在这种特殊情况下,它可以在 url http://github.com/mtai/python-gearman/tarball/master 中找到。但是,该链接本身不起作用,因为 easy_install 无法仅通过查看 URL 来判断它将获得什么。

By changing it to http://github.com/mtai/python-gearman/tarball/master#egg=gearman-2.0.0betainstead, easy_install will be able to identify the package name and its version.

通过将其更改为http://github.com/mtai/python-gearman/tarball/master#egg=gearman-2.0.0beta,easy_install将能够识别包名称及其版本。

The final step is to add the URL to your package's dependency_links, e.g.:

最后一步是将 URL 添加到包的 dependency_links,例如:

setup(
   ...
   dependency_links = ['http://github.com/mtai/python-gearman/tarball/master#egg=gearman-2.0.0beta']
)

Now, when YOUR package is being installed, easy_install will discover that there is a "gearman 2.0.0beta" available for download from that URL, and happily pick it over the one on PyPI, if you specify "gearman>=2.0.0beta" in your dependencies..

现在,当你的包被安装时,easy_install 会发现有一个“gearman 2.0.0beta”可以从那个 URL 下载,如果你指定“gearman>=2.0.0beta”,并且很高兴在 PyPI 上选择它在您的依赖项中..

(Normally, the way this sort of thing is done is to include a link on one's PyPI page to the downloadable source; in this case, if the author of the gearman package had included a link like the above, you'd be already set. Typically, people mark the development version with 'myproject-dev' and then people use a requirement of 'myproject>=somever,==dev', so that if there isn't a package of somever or higher, easy_install will try to check out or download the release.)

(通常,这种事情的完成方式是在一个人的 PyPI 页面上包含一个指向可下载源的链接;在这种情况下,如果 gearman 包的作者包含了一个像上面这样的链接,你就已经设置好了. 通常,人们用'myproject-dev'标记开发版本,然后人们使用'myproject>=somever,==dev'的要求,这样如果没有somever或更高版本的包,easy_install会尝试查看或下载版本。)

You'll need to specify --process-dependency-linkswhen using pip. Note that dependency links processing has been deprecated and will be removed in a future release.

您需要在使用--process-dependency-links时指定pip。请注意,依赖链接处理已被弃用,并将在未来版本中删除。

回答by Ned Deily

Vanilla setuptoolsdoes not support downloading directly from a git repository but you can use one of the Download Sourcelinks from that page, like:

Vanillasetuptools不支持直接从 git 存储库下载,但您可以使用该页面上的下载源链接之一,例如:

easy_install http://github.com/mtai/python-gearman/tarball/master

回答by Rebs

You can use the pip install protocol+location[@tag][#egg=Dependency]format to install directly from source using pip.

您可以使用该pip install protocol+location[@tag][#egg=Dependency]格式使用 pip 直接从源安装。

Git

吉特

pip install git+https://github.com/username/repo.git
pip install git+https://github.com/username/repo.git@MyTag
pip install git+https://github.com/username/repo.git@MyTag#egg=ProjectName

Mercurial

水银

pip install hg+https://hg.myproject.org/MyProject/

SVN

SVN

pip install svn+svn://svn.myproject.org/svn/MyProject

Bzr

溴化锆

pip install bzr+http://bzr.myproject.org/MyProject/trunk

The following protocols are supported: [+git, +svn, +hg, +bzr]

支持以下协议: [+git, +svn, +hg, +bzr]

Versions

版本

@taglets you specify a specific version/tag to check out.

@tag允许您指定要签出的特定版本/标签。

#egg=namelets you specify what the project is as a dependency for others.

#egg=name允许您指定项目是其他人的依赖项。

The order must always be @tag#egg=name.

顺序必须始终为@tag#egg=name

Private Repositories

私有仓库

You can also install from private repositories by changing the protocol to SSH (ssh://) and adding an appropriate user (git@):

您还可以通过将协议更改为 SSH ( ssh://) 并添加适当的用户 ( git@)从私有存储库进行安装:

git+ssh://[email protected]/username/my_private_repo

You can also install from private repositories with a username / password.

您还可以使用用户名/密码从私有存储库进行安装。

git+https://<username>:<password>@github.com/<user>/<repo>.git

Github provides the ability to create personal OAuth tokenswhich can be cycled

Github 提供了创建可以循环的个人 OAuth 令牌的能力

git+https://<oauth token>:[email protected]/<user>/<repo>.git

requirements.txt

要求.txt

requirements.txtis used to specify project dependencies:

requirements.txt用于指定项目依赖:

requirements.txt

要求.txt

package1
package2==1.0.2
package3>=0.0.4
git+https://github.com/username/repo.git

These are not installed automatically with the package and must be installed with the command pip -r requirements.txt.

这些不会随包自动安装,必须使用命令安装pip -r requirements.txt

Including requirements files

包括需求文件

Requirements files can include other requirements files:

需求文件可以包括其他需求文件:

requirements-docs.txt

要求-docs.txt

sphinx
-r requirements-dev.txt

requirements-dev.txt

需求-dev.txt

some-dev-tool
-r requirements.txt

requirements.txt

要求.txt

package1
package2==1.0.2
package3>=0.0.4
git+https://github.com/username/repo.git

setup.py

设置文件

Requirements files can install dependencies specified in setup.pywith the following command:

需求文件可以安装setup.py使用以下命令指定的依赖项:

-e .

setup.pycan also install from repositories using the same syntax as above, but using the dependency_linksvalue as mentioned in this answer.

setup.py也可以使用与上述相同的语法从存储库安装,但使用本答案中dependency_links提到的值。

References:

参考:

https://pip.pypa.io/en/latest/user_guide.html#installing-packageshttps://pip.pypa.io/en/latest/reference/pip_install.html

https://pip.pypa.io/en/latest/user_guide.html#installing-packages https://pip.pypa.io/en/latest/reference/pip_install.html

回答by Phil

As I just had to do the same thing, I found another way to do this as pip's --process-dependency-linksare scheduled to be removed in pip19.0 according to this comment.

正如我刚刚做同样的事情,我发现了另一种方式来做到这一点作为pip--process-dependency-links被安排在除去pip按照19.0此评论

pip18.1 includes the following feature

pip18.1 包括以下功能

Allow PEP 508 URL requirements to be used as dependencies.

允许将 PEP 508 URL 要求用作依赖项。

From the descriptionof PEP 508, the syntax for such URL dependencies looks like:

根据PEP 508的描述,此类 URL 依赖项的语法如下所示:

A minimal URL based lookup:

pip @ https://github.com/pypa/pip/archive/1.3.1.zip#sha1=da9234ee9982d4bbb3c72346a6de940a148ea686

一个最小的基于 URL 的查找:

pip @ https://github.com/pypa/pip/archive/1.3.1.zip#sha1=da9234ee9982d4bbb3c72346a6de940a148ea686

So in your setup.pyit would look like

所以在你setup.py看来

setup(
   ...
   install_requires = [
   ...
   'python-gearman @ https://github.com/mtai/python-gearman/archive/master.zip'
   ...
   ]
)

Notice, the link is an archive file and could also be a specific release or branch of a repository as described in this answer. Also, see that answer for working with other repository hosts.

请注意,该链接是一个存档文件,也可以是此答案中所述的特定版本或存储库的分支。另外,请参阅与其他存储库主机一起工作的答案。

To the best of my knowledge, the easiest way to update the dependency is by using pip install -I .when installing your package from its directory.

据我所知,更新依赖项的最简单方法是pip install -I .在从其目录安装包时使用。