Python 如何编写 setup.py 以包含 git repo 作为依赖项

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

How to write setup.py to include a git repo as a dependency

pythondjangogitpackagingsetuptools

提问by Ankur Agarwal

I am trying to write setup.pyfor my package. My package needs to specify a dependency on another git repo.

我正在尝试setup.py为我的包裹写作。我的包需要指定对另一个 git repo 的依赖。

This is what I have so far:

这是我到目前为止:

from setuptools import setup, find_packages

setup(
    name='abc',
    packages=find_packages(),
    url='https://github.abc.com/abc/myabc',
    description='This is a description for abc',
    long_description=open('README.md').read(),
    install_requires=[
        "requests==2.7.0",
        "SomePrivateLib>=0.1.0",
        ],
    dependency_links = [
     "git+git://github.abc.com/abc/SomePrivateLib.git#egg=SomePrivateLib",
    ],
    include_package_data=True,
)

When I run:

当我运行时:

pip install -e https://github.abc.com/abc/myabc.git#egg=analyse

I get

我得到

Could not find a version that satisfies the requirement SomePrivateLib>=0.1.0 (from analyse) (from versions: ) No matching distribution found for SomePrivateLib>=0.1.0 (from analyse)

找不到满足 SomePrivateLib>=0.1.0 要求的版本(来自分析)(来自版本:)找不到 SomePrivateLib>=0.1.0 的匹配发行版(来自分析)

What am I doing wrong ?

我究竟做错了什么 ?

采纳答案by cel

You can find the right way to do it here.

您可以在此处找到正确的方法。

dependency_links=['http://github.com/user/repo/tarball/master#egg=package-1.0']

The key is not to give a link to a git repository, but a link to a tarball. Github creates a tarball of the master branch for you if you append /tarball/masteras shown above.

关键不是提供一个指向 git 存储库的链接,而是一个指向 tarball 的链接。如果您/tarball/master按如上所示进行追加,Github 会为您创建主分支的 tarball 。

回答by tedivm

Unfortunately the other answer does not work with private repositories, which is one of the most common use cases for this. I eventually did get it working with a setup.pyfile that looks like this:

不幸的是,另一个答案不适用于私有存储库,这是最常见的用例之一。我最终确实让它与一个setup.py看起来像这样的文件一起工作:

from setuptools import setup, find_packages

setup(
    name = 'MyProject',
    version = '0.1.0',
    url = '',
    description = '',
    packages = find_packages(),
    install_requires = [
        # Github Private Repository - needs entry in `dependency_links`
        'ExampleRepo'
    ],

    dependency_links=[
        # Make sure to include the `#egg` portion so the `install_requires` recognizes the package
        'git+ssh://[email protected]/example_organization/ExampleRepo.git#egg=ExampleRepo-0.1'
    ]
)

Newer versions of pip make this even easier by removing the need to use "dependency_links"-

较新版本的 pip 通过消除使用“dependency_links”的需要使这变得更加容易-

from setuptools import setup, find_packages

setup(
    name = 'MyProject',
    version = '0.1.0',
    url = '',
    description = '',
    packages = find_packages(),
    install_requires = [
        # Github Private Repository
        'ExampleRepo @ git+ssh://[email protected]/example_organization/ExampleRepo.git#egg=ExampleRepo-0.1'
    ]
)

回答by Dick Fox

After digging through the pip issue 3939linked by @muon in the comments above and then the PEP-508 specification, I found success getting my private repo dependency to install via setup.pyusing this specification pattern in install_requires(no more dependency_links):

在挖掘了上面评论中@muon 链接的pip 问题 3939以及PEP-508 规范之后,我发现通过setup.pyinstall_requires(不再dependency_links)中使用此规范模式成功安装了我的私有仓库依赖项:

install_requires = [
  'some-pkg @ git+ssh://[email protected]/someorgname/[email protected]#egg=some-pkg',
]

The @v1.1indicates the release tag created on github and could be replaced with a branch, commit, or different type of tag.

@v1.1指示在GitHub上创建的发布标志,并可以用一个分支,提交,或不同类型的标签来代替。

回答by Gonzalo Odiard

A more general answer, to get the information from the requeriments.txt file I do:

一个更一般的答案,要从 requeriments.txt 文件中获取信息,我会这样做:

from setuptools import setup, find_packages
from os import path

loc = path.abspath(path.dirname(__file__))

with open(loc + '/requirements.txt') as f:
    requirements = f.read().splitlines()

required = []
dependency_links = []
# do not add to required lines pointing to git repositories
EGG_MARK = '#egg='
for line in requirements:
    if line.startswith('-e git:') or line.startswith('-e git+') or \
            line.startswith('git:') or line.startswith('git+'):
        if EGG_MARK in line:
            package_name = line[line.find(EGG_MARK) + len(EGG_MARK):]
            required.append(package_name)
            dependency_links.append(line)
        else:
            print('Dependency to a git repository should have the format:')
            print('git+ssh://[email protected]/xxxxx/xxxxxx#egg=package_name')
    else:
        required.append(line)

setup(
    name='myproject',  # Required
    version='0.0.1',  # Required
    description='Description here....',  # Required
    packages=find_packages(),  # Required
    install_requires=required,
    dependency_links=dependency_links,
) 

回答by darencorp

Actually if you like to make your packages installable recursivelly (YourCurrentPackage includes your SomePrivateLib), e.g. when you want to include YourCurrentPackage into another one (like OuterPackage -> YourCurrentPackage -> SomePrivateLib) you'll need both:

实际上,如果您想让您的软件包可递归安装(YourCurrentPackage 包括您的 SomePrivateLib),例如,当您想将 YourCurrentPackage 包含到另一个软件包中时(例如 OuterPackage -> YourCurrentPackage -> SomePrivateLib),您将需要两者:

install_requires=[
    ...,
    "SomePrivateLib @ git+ssh://github.abc.com/abc/[email protected]#egg=SomePrivateLib"
],
dependency_links = [
    "git+shh://github.abc.com/abc/[email protected]#egg=SomePrivateLib"
]

And make sure you have a tag created with your version number.

并确保您使用版本号创建了一个标签。

Also if your git project is private and you like to install it inside the container e.g. Docker or GitLab runner, you will need authorized access to your repo. Please consider to use git+https with access tokens (like on GitLab: https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html):

此外,如果您的 git 项目是私有的,并且您想将其安装在容器中,例如 Docker 或 GitLab runner,您将需要对您的存储库进行授权访问。请考虑将 git+https 与访问令牌一起使用(例如在 GitLab 上:https: //docs.gitlab.com/ee/user/profile/personal_access_tokens.html ):

install_requires=[
        ...,
        "SomePrivateLib @ git+https://gitlab-ci-token:{EXPORTED_VAR_WITH_TOKEN}@gitlab.server.com/abc/[email protected]#egg=SomePrivateLib"
    ],
dependency_links = [
        "git+https://gitlab-ci-token:{EXPORTED_VAR_WITH_TOKEN}@gitlab.server.com/abc/[email protected]#egg=SomePrivateLib"
    ]