微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Pipfile 中指定特定于平台的附加功能或版本?

如何解决如何在 Pipfile 中指定特定于平台的附加功能或版本?

"Advanced Usage of Pipenv" docs,在“指定基本任何东西”部分,解释了如何在 Pipfile 中包含一个包要求,有条件地在 os 平台上,例如:

pywinusb = {version = "*",sys_platform = "== 'win32'"}

当你想根据平台为一个包指定不同的版本或选项时,不清楚该怎么做。

具体来说,我有这个要求:

faust = {version=">=1.10.1",extras=["aiodns","ciso8601","cython"]}

并且我想在 Windows 中排除可选的额外 ciso8601

如果我写

faust = {version=">=1.10.1","cython"],platform_system = "!= 'Windows'"}
faust = {version=">=1.10.1",platform_system = "== 'Windows'"}

我收到错误tomlkit.exceptions.KeyAlreadyPresent: Key "faust " already exists.

此外,我想在 setup.pyinstall_requires 部分)中做同样的事情。

解决方法

工作答案 - 06-30-2021 @15:43 UTC

我多次查看 faust 存储库,发现 aiodnscythonciso8601 不是依赖项本身,但是是捆绑包,根据自述文件以这种方式安装 pip:

pip install "faust[aiodns,ciso8601,cython]"

我根据您的要求创建了一个 setup.py。我能够在非 Windows 系统上安装 faustaiodnsciso8601cython 包。我没有 Windows 系统来测试该安装,但我确实翻转了系统平台参数,这奏效了。

import setuptools

setuptools.setup(

    name="faust_pip_test",# change for your package
    version="1.0.0",# change for your package
    python_requires='>=3.6',install_requires=["faust[aiodns,cython]>=1.10.1;sys_platform != 'win32'","faust[aiodns,cython]>=1.10.1;sys_platform == 'win32'",]

)

这是与 faust 一起安装的 faust_pip_test 包和其他包 aiodnsciso8601cython 在我的 MacBook 上。

enter image description here

查看 PipeEnv 文档后,这并不直观,您似乎可以在 pipfile 中执行以下操作。我查看的每个参考资料都要求您为辅助密钥指定一个文件路径,在这个原因中是 faust_winfaust_win 中调用的文件来自 faust PyPi downloads

[packages]
faust = {version = ">=1.10.1",extras = ["aiodns","ciso8601","cython"],sys_platform = "!='windows'"}
faust_win = {file = 'https://files.pythonhosted.org/packages/79/f8/3fec4f5c3e5bf1ce8bb557ae507525253fa30a5cfc5984f342b931143f75/faust-1.10.4-py2.py3-none-any.whl',version = ">=1.10.1",sys_platform = "=='windows'"}

我的开发环境使用 PyCharm。如果我在我的项目 faust_pipenv_test 中创建一个 __init__.py,它会自动触发我的项目的依赖项安装。安装了 Faust standard dependencies requirements 以及其他软件包 aiodnsciso8601cython。

enter image description here

我还收到一条消息,指出未安装 faust_win 要求,因为它不符合此要求 sys_platform =='win32'

enter image description here

更新 06-28-2021 @12:23 UTC

当我查看 faust 的源代码时,我注意到包 aiodnscythonciso8601faust 的 setup.py 文件中的依赖项。

所以根据您的问题,您似乎不想安装 faust ciso8601 对非 Windows 系统的默认依赖。根据附加列表,您似乎也在尝试限制其他依赖项。

这是您想要完成的吗?

Python 开发人员指南指出,"extras" 参数用于安装额外的依赖项,这是对常规依赖项的补充正在安装的 Python 包。

参考:PEP 426 -- Metadata for Python Software Packages 2.0

下面的代码来自 PipeEnv Python 包,它表明 "extras" 参数与包的核心依赖项相结合。

def dependencies(self):
        # type: () -> Tuple[Dict[S,PackagingRequirement],List[Union[S,PackagingRequirement]],List[S]]
        build_deps = []  # type: List[Union[S,PackagingRequirement]]
        setup_deps = []  # type: List[S]
        deps = {}  # type: Dict[S,PackagingRequirement]
        if self.setup_info:
            setup_info = self.setup_info.as_dict()
            deps.update(setup_info.get("requires",{}))
            setup_deps.extend(setup_info.get("setup_requires",[]))
            build_deps.extend(setup_info.get("build_requires",[]))
            if self.extras and self.setup_info.extras:
                for dep in self.extras:
                    if dep not in self.setup_info.extras:
                        continue
                    extras_list = self.setup_info.extras.get(dep,[])  # type: ignore
                    for req_instance in extras_list:  # type: ignore
                        deps[req_instance.key] = req_instance
        if self.pyproject_requires:
            build_deps.extend(list(self.pyproject_requires))
        setup_deps = list(set(setup_deps))
        build_deps = list(set(build_deps))
        return deps,setup_deps,build_deps

所以下面的例子需要测试,因为 Advanced Usage of Pipenv 文档中没有明确说明是否可以覆盖第三方 Python 包的依赖项要求。

[packages]
faust = {version= ">=1.10.1",extras=["aiodns","cython"]}
ciso8601 = {version = ">=2.1.0",sys_platform = "!= 'Windows'",index="pypi"}

or 

[packages]
faust = {version=">=1.10.1",sys_platform= "!= 'Windows'"}
"faust_win" = {version=">=1.10.1",sys_platform = "== 'Windows'"}

原帖 06-26-2021


TOML 文档中不允许出现重复键。

例如这个:

faust = {version=">=1.10.1",platform_system = "!= 'Windows'"}

faust = {version=">=1.10.1",platform_system = "== 'Windows'"}

参考pipenv的源代码

# file: /pipenv/vendor/tomlkit/exceptions.py
# which is referenced from here /pipenv/vendor/tomlkit/container.py

class KeyAlreadyPresent(TOMLKitError):
    """
    An already present key was used.
    """

    def __init__(self,key):
        message = 'Key "{}" already exists.'.format(key)

        super(KeyAlreadyPresent,self).__init__(message)

此外,TOML Github 存储库中还讨论了重复密钥问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。