是否可以欺骗 pip install --find-links 使用下载的 sdist 来满足 --editable 要求?

发布于 2024-12-09 15:45:26 字数 735 浏览 1 评论 0原文

使用以下命令:

pip install -r requirements.txt -d sdists/

您可以轻松创建需求存档以随项目一起分发。如果您的需求如下所示,那么这非常有用:

Django==1.3.1
django-tagging==0.3.1
django-robots==0.6.1

然后您可以在完全不接触 PyPI 的情况下安装这些需求,如下所示:

pip install -r requirements.txt --find-links sdists/ --no-index

Is it possible to use the same method for --editable requests?例如:

-e hg+https://bitbucket.org/ubernostrum/django-contact-form/@1d3791fa4dfb#egg=django-contact-form

据我所知,pip install -d 很乐意下载可编辑的需求并为您创建一个 sdist,但是 pip install --find-links 没有任何将下载的 sdist 与需求文件中的相关行进行匹配的方法,因此它会忽略下载的 sdist 并继续像往常一样从 VCS 中检查代码。

Using the following command:

pip install -r requirements.txt -d sdists/

You can easily create an archive of requirements for distributing with your project. This works great if your requirements look like this:

Django==1.3.1
django-tagging==0.3.1
django-robots==0.6.1

You can then install those requirements without touching PyPI at all, like so:

pip install -r requirements.txt --find-links sdists/ --no-index

Is it possible to use the same method for --editable requirements? E.g.:

-e hg+https://bitbucket.org/ubernostrum/django-contact-form/@1d3791fa4dfb#egg=django-contact-form

As far as I can tell, pip install -d happily downloads editable requirements and creates an sdist for you, but pip install --find-links does not have any way to match up the downloaded sdist with the associated line in your requirements file, so it ignores the downloaded sdist and continues checking out the code from VCS as usual.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花似海 2024-12-16 15:45:26

虽然使用 PIP 似乎不可能完全实现这一点,但有一种解决方法可以完成相同的任务。解决方法是从原始需求文件和 sdists 目录(仅用于该目录)自动生成第二个需求文件。

一个简单的实现可能看起来像这样(保存在名为“make_reqs.py”的文件中):

#!/usr/bin/env python

import re
import sys
import os.path

pat = '.+#egg=(.+)'
allowed_exts = ['.zip', '.tar.gz', 'tar.bz2']

def find_version(sdists_dir, name):
    files = [f for f in os.listdir(sdists_dir) if f.startswith(name)]
    if len(files) != 1:
        return ''
    version = files[0].replace(name+'-', '')
    for ext in allowed_exts:
        version = version.replace(ext, '')
    return version

def get_requirements(file_name, sdists_dir):
    out_reqs = ['--find-links file://%s' % os.path.abspath(sdists_dir)]
    with open(file_name) as req_file:
        reqs = [l.strip() for l in req_file.readlines()]
        for req in reqs:
            match = re.match(pat, req)
            if match and not req.startswith('#'):
                name = match.group(1)
                version = find_version(sdists_dir, name)
                if version:
                    out_reqs.append('%s==%s' % (name, version))
                else:
                    out_reqs.append(req)
            else:
                out_reqs.append(req)
    return out_reqs

if __name__ == '__main__':
    print '\n'.join(get_requirements(*sys.argv[1:]))

要使用该脚本,您需要执行以下操作:

python make_reqs.py requirements.txt /path/to/sdists > sdist_reqs.txt
pip install --no-index -r sdist_reqs.txt

While it doesn't appear that this is strictly possible using PIP, there is a workaround that accomplishes the same thing. The workaround is to automatically generate a second requirements file from the original requirements file and sdists directory (to be used only for that directory).

A simple implementation might look something like this (save in a file called "make_reqs.py"):

#!/usr/bin/env python

import re
import sys
import os.path

pat = '.+#egg=(.+)'
allowed_exts = ['.zip', '.tar.gz', 'tar.bz2']

def find_version(sdists_dir, name):
    files = [f for f in os.listdir(sdists_dir) if f.startswith(name)]
    if len(files) != 1:
        return ''
    version = files[0].replace(name+'-', '')
    for ext in allowed_exts:
        version = version.replace(ext, '')
    return version

def get_requirements(file_name, sdists_dir):
    out_reqs = ['--find-links file://%s' % os.path.abspath(sdists_dir)]
    with open(file_name) as req_file:
        reqs = [l.strip() for l in req_file.readlines()]
        for req in reqs:
            match = re.match(pat, req)
            if match and not req.startswith('#'):
                name = match.group(1)
                version = find_version(sdists_dir, name)
                if version:
                    out_reqs.append('%s==%s' % (name, version))
                else:
                    out_reqs.append(req)
            else:
                out_reqs.append(req)
    return out_reqs

if __name__ == '__main__':
    print '\n'.join(get_requirements(*sys.argv[1:]))

To use the script, you would do something like this:

python make_reqs.py requirements.txt /path/to/sdists > sdist_reqs.txt
pip install --no-index -r sdist_reqs.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文