pip required.txt基于环境变量的条件或环境标记 [英] pip requirement.txt conditionals or environment markers based on env variables

查看:161
本文介绍了pip required.txt基于环境变量的条件或环境标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以根据环境变量的值在pip requirements.txt文件中指定条件安装?

Is there a way to specify conditional installs in a pip requirements.txt file that are based on the value of an environment variable?

我已经可以使用环境标记来控制大部分需求,但是我所知道的所有标记都只能使用本质上由python预先定义的特定值.

I've been able to control most of what I need with environment markers, but all the markers I know of work only with specific values that are essentially pre-defined by python.

例如,我希望能够控制RHEL 5.3 vs. RHEL 6.3 vs. RHEL 6.6等的软件包安装.同样基于其他条件.

For example, I want to be able to control package installation for RHEL 5.3 vs. RHEL 6.3 vs. RHEL 6.6, etc. Also based on other criteria.

如果我可以在results.txt文件中指定一个环境变量,该变量将在运行pip之前根据我设置的值进行检查,那将是完美的.这似乎是理想且直接的功能.到目前为止,我对找到有关环境标记的全面讨论还不太幸运,所以我希望我只是错过了一些关键信息:-)

It would be perfect if I could specify in the results.txt file an environment variable that would be checked against a value I set before running pip. This seems like it would be desirable and straight forward functionality. I haven't had much luck so far finding comprehensive discussions of environment markers, so I'm hoping I've just missed some key bit of information :-)

非常感谢.

推荐答案

实际上并没有一种使用环境变量的方法.点子要求文件基本上只是文件中列出的pip install参数的列表.因此,如果您的需求文件如下所示:

There's not really a way to do it with environment variables. Pip requirements files are basically just a list of pip install arguments listed in a file. So if your requirements file looks like this:

Foo==1.1.0
Bar==0.1.0
Baz==2.0.1

从逻辑上讲,pip是这样做的:

Logically, pip is doing this:

pip install Foo==1.1.0
pip install Bar==0.1.0
pip install Baz==2.0.1

不幸的是,在这种情况下,无法应用环境变量.

Unfortunately, in that context there's no way to apply environment variables.

有两个解决方案.

一个,您可能有一个基本需求文件,例如requirements.txt,其中列出了所有平台的常见依赖关系.然后,您可以将各个需求文件命名为requirements.rhel53.txtrequirements.rhel63.txt等.每个文件的顶部都可以将其作为第一行:

One, you could have a base requirements file, say requirements.txt, that lists common dependencies for all platforms. Then, you could have individual requirements files that are named, say, requirements.rhel53.txt, requirements.rhel63.txt, etc. The top of each of these files could have this as the first line:

-r requirements.txt

,然后列出其他特殊依赖项.然后,在每种环境中,您都可以设置一个env var,将其称为$PLATFORM,然后运行类似这样的命令来安装依赖项:

And then additional special dependencies listing. Then, in each environment, you could set an env var, let's call it $PLATFORM, and run a command like this to install dependencies:

$ pip install -r requirements.$PLATFORM.txt

或者,您可以使用约束文件.您的requirements.txt只会列出没有版本的依赖项:

Or, you could use constraints files. Your requirements.txt would just list dependencies without versions:

Foo
Bar
Baz

然后您可能会再次有一个约束文件,该文件针对每个平台,其中列出了特定的版本要求.例如,您可能有constraints.rhel53.txt具有以下内容:

And then you could have a constraints file, again for each platform, that would list specific version requirements. For example, you could have constraints.rhel53.txt that had this:

Foo==1.1.0
Bar==0.1.0
Baz==2.0.1

再次,您设置一个env var,然后运行如下命令:

And again, you set an env var and then run a command like this:

$ pip install -r requirements.txt -c constraints.$PLATFORM.txt

这是一个麻烦的解决方案,但这将是处理它的一种方式.不幸的是,pip没有本机解决方案.

It's a cumbersome solution, but that would be one way of dealing with it. Unfortunately, pip does not have a native solution.

这篇关于pip required.txt基于环境变量的条件或环境标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆