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

2024-06-01 20:57:57 发布

您现在位置:Python中文网/ 问答频道 /正文

"Advanced Usage of Pipenv" docs中的“基本上指定任何内容”部分,解释了如何在Pipfile中有条件地在操作系统平台上包括包需求,例如:

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

根据平台的不同,当需要为包指定不同的版本或选项时,不清楚该怎么做

具体而言,我有以下要求:

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

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

如果我写

faust = {version=">=1.10.1", extras=["aiodns", "ciso8601", "cython"], platform_system = "!= 'Windows'"}
faust = {version=">=1.10.1", extras=["aiodns", "cython"], platform_system = "== 'Windows'"}

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

此外,我希望在setup.pyinstall_requires部分中有同样的内容


Tags: ofextras内容versionwindowspipenvusage平台
1条回答
网友
1楼 · 发布于 2024-06-01 20:57:57

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

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

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

我根据您的要求创建了setup.py。我能够在非windows系统上安装带有捆绑包的faust,aiodnsciso8601cython。我没有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, ciso8601,cython]>=1.10.1;sys_platform != 'win32'",
                  "faust[aiodns, cython]>=1.10.1;sys_platform == 'win32'",]

)

下面是安装在我的MacBook上的faustfaust\u pip\u test软件包和附加软件包aiodnsciso8601cython

enter image description here

在查看了PipeEnv文档(这不是直观的)之后,您似乎可以在PIP文件中执行以下操作。我查看的每个参考资料都要求您为次键指定一个文件路径,因此它是faust_win。在faust_-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",extras = ["aiodns", "cython"],sys_platform = "=='windows'"}

我正在使用PyCharm作为我的开发环境。如果我在我的项目中创建一个__init__.py,它会自动触发我的项目的依赖项安装<安装了em>Fauststandard dependencies requirements以及额外的软件包aiodnsciso8601cython

enter image description here

我还收到一条消息说,faust_win要求没有安装,因为它不符合这个要求

enter image description here

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

当我查看faust的源代码时,我注意到 aiodnscythonciso8601是faustsetup.py文件中的依赖项

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

这就是你想要实现的吗?

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", extras=["aiodns", "ciso8601", "cython"], sys_platform= "!= 'Windows'"}
"faust_win" = {version=">=1.10.1", extras=["aiodns", "cython"], sys_platform = "== 'Windows'"}

原邮政编码:06-26-2021


TOML文档中不允许重复密钥

例如:

faust = {version=">=1.10.1", extras=["aiodns", "ciso8601", "cython"], platform_system = "!= 'Windows'"}

faust = {version=">=1.10.1", extras=["aiodns", "cython"], 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)

此外,在TOMLGithub存储库中还讨论了重复密钥问题

相关问题 更多 >