pythonpackagingpython-poetry

Poetry: how to specify the same dependency twice with different CPU architectures (e.g x86 and x86_64)


I am evaluating using Poetry for packaging and building a desktop application.

The only roadblocker is that poetry doesn't seam to allow specifying the same package twice. For instance I couldn't do the following:

[tool.poetry.dependencies]
python = "^3.9"
lru-dict = {path = "./packages/lru_dict-1.1.6-cp39-cp39-win_amd64.whl"}
lru-dict = {path = "./packages/lru_dict-1.1.6-cp39-cp39-win32.whl"}

Notice that the lru-dict package is specified twice with the only difference being the bitness (i.e. the CPU architecture) that the package is built for.

I know I can upload the package to PyPI and pip will choose the appropriate version dynamically. But what about private or local packages?


Solution

  • From the poetry documentation:

    Poetry supports environment markers via the markers property.

    One of those markers is platform_machine, which is the output of platform.machine().

    So you should be able to do something like this:

    [tool.poetry.dependencies]
    python = "^3.9"
    lru-dict = [
        { path = "./packages/lru_dict-1.1.6-cp39-cp39-win_amd64.whl", markers = "platform_machine == 'amd64'" },
        { path = "./packages/lru_dict-1.1.6-cp39-cp39-win32.whl", markers = "platform_machine == 'win32'" }
    ]
    

    Note that the package name appears only once, but is assigned a list of specifications for that package, in this case differing in path and markers. You cannot have the same package name twice in your dependencies, not unlike how you cannot have the same key twice in a Python dictionary.