21

当用户发出安装原始软件的命令时,我希望 pip 安装我在 GitHub 上的依赖项,也来自 GitHub 上的源代码。这些包都没有在 PyPi 上(永远不会)。

用户发出命令:

pip -e git+https://github.com/Lewisham/cvsanaly@develop#egg=cvsanaly

这个 repo 有一个requirements.txt文件,另一个依赖于 GitHub:

-e git+https://github.com/Lewisham/repositoryhandler#egg=repositoryhandler

我想要的是用户可以发出一个命令来安装原始包,让 pip 找到需求文件,然后也安装依赖项。

4

3 回答 3

36

这个答案帮助我解决了你所说的同样的问题。

setup.py 似乎没有一种简单的方法可以直接使用需求文件来定义其依赖项,但是可以将相同的信息放入 setup.py 本身。

我有这个 requirements.txt:

PIL
-e git://github.com/gabrielgrant/django-ckeditor.git#egg=django-ckeditor

但是在安装 requirements.txt 包含的包时,pip 会忽略这些要求。

这个 setup.py 似乎强制 pip 安装依赖项(包括我的 github 版本的 django-ckeditor):

from setuptools import setup

setup(
    name='django-articles',
    ...,
    install_requires=[
        'PIL',
        'django-ckeditor>=0.9.3',
    ],
    dependency_links = [
        'http://github.com/gabrielgrant/django-ckeditor/tarball/master#egg=django-ckeditor-0.9.3',
    ]
)

编辑:

这个答案还包含一些有用的信息。

需要将版本指定为“#egg=...”的一部分,以标识链接中可用的软件包版本。但是请注意,如果您总是想依赖最新版本,您可以dev在 install_requires、dependency_links 和其他包的 setup.py中设置版本

编辑:根据下面的评论,使用dev作为版本不是一个好主意。

于 2010-12-19T12:56:55.430 回答
13

这是我用来从需求文件生成install_requires的小脚本。dependency_links

import os
import re

def which(program):
    """
    Detect whether or not a program is installed.
    Thanks to http://stackoverflow.com/a/377028/70191
    """
    def is_exe(fpath):
        return os.path.exists(fpath) and os.access(fpath, os.X_OK)

    fpath, _ = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ['PATH'].split(os.pathsep):
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file

    return None

EDITABLE_REQUIREMENT = re.compile(r'^-e (?P<link>(?P<vcs>git|svn|hg|bzr).+#egg=(?P<package>.+)-(?P<version>\d(?:\.\d)*))$')

install_requires = []
dependency_links = []

for requirement in (l.strip() for l in open('requirements')):
    match = EDITABLE_REQUIREMENT.match(requirement)
    if match:
        assert which(match.group('vcs')) is not None, \
            "VCS '%(vcs)s' must be installed in order to install %(link)s" % match.groupdict()
        install_requires.append("%(package)s==%(version)s" % match.groupdict())
        dependency_links.append(match.group('link'))
    else:
        install_requires.append(requirement)
于 2012-02-03T07:55:32.813 回答
1

这回答了你的问题了吗?

setup(name='application-xpto',
  version='1.0',
  author='me,me,me',
  author_email='xpto@mail.com',
  packages=find_packages(),
  include_package_data=True,
  description='web app',
  install_requires=open('app/requirements.txt').readlines(),
  )
于 2012-06-18T19:35:47.817 回答