简体   繁体   English

python2; 使用--trusted-host和--extra-index-url pip parse_requirements

[英]python2; pip parse_requirements with --trusted-host and --extra-index-url

I am using 我在用

install_requires = [str(ir.req) for ir in parse_requirements("requirements.txt", session=PipSession())]

with pip install . pip install . . However, this does not seem to work with a requirements.txt that looks like this: 但是,这似乎不适用于看起来像这样的requirements.txt:

--trusted-host blah
--extra-index-url blah2
...

(support for --trusted-host was added in pip8.0.0). (在pip8.0.0中添加了对--trusted-host的支持)。 The install from blah fails because it complains about it not being an untrusted host as if it never processed the first line. 从blah安装失败,因为它抱怨它不是一个不受信任的主机,好像它从未处理过第一行。

HOWEVER, pip install -r requirements.txt works perfectly, so these options are correct. 但是, pip install -r requirements.txt工作正常,所以这些选项都是正确的。

This means there is something parse_requirements is not doing. 这意味着parse_requirements没有做的事情。 My question is : how do I fix or work around this using only pip install . 我的问题是 :如何使用pip install .修复或解决此问题pip install . ? I could do something horrendous like: 我可以做一些可怕的事情:

os.system(pip install -r requirements.txt)
setup(...

in the setup.py file. 在setup.py文件中。

The implicit coupling of requirements.txt and setup.py is confusing to me. requirements.txt和setup.py的隐式耦合让我很困惑。 Nothing in setup calls requirements.txt unless you explicitly parse requirements.txt yourself, yet requirements.txt is a very standard python convention. setup中没有任何内容调用requirements.txt,除非您自己显式解析requirements.txt,但requirements.txt是一个非常标准的python约定。

EDIT: We are using tools (Cloudify and sometimes Chef) that execute a pip install . 编辑:我们正在使用执行pip install .工具(Cloudify,有时是Chef) pip install . . We cannot change this. 我们无法改变这一点。 I have to get this working as a pippable package, with the --trusted-host and --extra-index-urls without using a pip.conf either. 我必须使用--trusted-host和--extra-index-urls而不使用pip.conf来实现这个可用包。 Currently we are doing the os.system trick. 目前我们正在做os.system技巧。

There has been much written about using setup.py vrs. 有很多关于使用setup.py vrs的文章。 requirements.txt. requirements.txt。 Setup.py is for Abstract requirements. Setup.py适用于抽象需求。 Requirements.txt is for concrete requirements. Requirements.txt用于具体要求。 In other words, they serve different purposes. 换句话说,它们用于不同的目的。 Whereas requirements.txt is for an environment, setup.py is for a package. 而requirements.txt适用于环境,而setup.py适用于包。 So it doesn't make sense for a setup.py to read from a requirement.txt just like it wouldn't make sense for a deb package to read from a Chef cookbook. 因此,setup.py从requires.txt中读取就没有意义,就像deb包从Chef菜谱中读取一样没有意义。 Abstract vrs. 抽象的vrs。 Concrete Requirements 具体要求

Often the reason people do this is they want to support people installing their package with pip install -r requirements.txt from within a check out without needing to list their dependencies twice. 通常人们这样做的原因是他们希望支持人们在签出时使用pip install -r requirements.txt安装他们的包,而不需要两次列出他们的依赖项。 That's a reasonable thing to want which is why the requirements file format has a construct that enables it, simply make a requirements.txt file that contains "." 这是一个合理的事情,这就是为什么需求文件格式有一个启用它的构造,只需创建一个包含“。”的requirements.txt文件。 or "-e ." 或“-e”。 and pip will automatically install the project and all of it's dependencies. 和pip将自动安装项目及其所有依赖项。

EDIT: Since pip is not a library, using the most exposed part of the program is the safest (in my opinion). 编辑:由于pip不是一个库,使用程序中最暴露的部分是最安全的(在我看来)。 An alternative to os.system is os.system的替代方案是

import pip
pip.main(['install','-r','requirements.txt'])

This answer from goCards is perfectly valid and you should probably import pip from your setup.py if there's no way around pip install . 来自goCards的答案是完全有效的,你可能应该从你的setup.py导入pip,如果pip install .没有办法解决的话pip install . . But I would like to explain what actually happens here. 但我想解释一下这里到底发生了什么。 Here's what you need to know: 这是你需要知道的:

  1. install_requires is an option only supported by setuptools, a third-party package that improves upon distutils (the standard tool used in setup.py files and distributed with Python). install_requires是setuptools唯一支持的选项,它是改进distutils的第三方软件包(setup.py文件中使用的标准工具,随Python一起分发)。
  2. By design, setuptools only accepts actual requirements in install_requires , so options such as --trusted-host cannot be sent to install_requires . 根据设计,setuptools 只接受install_requires实际要求 ,因此无法将诸如--trusted-host选项发送到install_requires
  3. This is why you're using parse_requirements("requirements.txt", session=PipSession()) . 这就是你使用parse_requirements("requirements.txt", session=PipSession()) This function only yields packages. 此函数仅生成包。 The option lines such as --trusted-host bla are not returned, but sent to a PackageFinder if you gave one to parse_requirements . 诸如--trusted-host bla类的选项行不会被返回,但是如果你给了一个parse_requirements则会发送给PackageFinder But you don't want these options to be returned, because setuptools would not know what do with those! 但是你不希望返回这些选项,因为setuptools不知道这些选项有什么用!

Long story short, if you want to use pip options, you need to use pip. 长话短说,如果你想使用pip选项,你需要使用pip。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM