rules_py icon indicating copy to clipboard operation
rules_py copied to clipboard

[Bug]: `py_pex_binary` fails with `AmbiguousDistributionError`

Open harshalparekh6 opened this issue 1 year ago • 2 comments

What happened?

Building py_pex_binary fails with:

Traceback (most recent call last):
  File "/home/playground/.cache/bazel/_bazel_playground/33746cb7bfc65ca42552dbe5cbc93007/execroot/ntropy/bazel-out/k8-opt-exec-2B5CBBC6/bin/external/aspect_rules_py/py/tools/pex/pex.runfiles/aspect_rules_py/py/tools/pex/main.py", line 162, in <module>
    dist = Distribution.load(dep + "/../")
  File "/home/playground/.cache/bazel/_bazel_playground/33746cb7bfc65ca42552dbe5cbc93007/execroot/ntropy/bazel-out/k8-opt-exec-2B5CBBC6/bin/external/aspect_rules_py/py/tools/pex/pex.runfiles/.pex.venv/lib/python3.9/site-packages/pex/dist_metadata.py", line 885, in load
    return cls(location=location, metadata=DistMetadata.load(location))
  File "/home/playground/.cache/bazel/_bazel_playground/33746cb7bfc65ca42552dbe5cbc93007/execroot/ntropy/bazel-out/k8-opt-exec-2B5CBBC6/bin/external/aspect_rules_py/py/tools/pex/pex.runfiles/.pex.venv/lib/python3.9/site-packages/pex/dist_metadata.py", line 805, in load
    metadata_files = load_metadata(location, restrict_types_to=restrict_types_to)
  File "/home/playground/.cache/bazel/_bazel_playground/33746cb7bfc65ca42552dbe5cbc93007/execroot/ntropy/bazel-out/k8-opt-exec-2B5CBBC6/bin/external/aspect_rules_py/py/tools/pex/pex.runfiles/.pex.venv/lib/python3.9/site-packages/pex/dist_metadata.py", line 363, in load_metadata
    raise AmbiguousDistributionError(
pex.dist_metadata.AmbiguousDistributionError: Found more than one distribution inside external/python3_9_x86_64-unknown-linux-gnu/lib/python3.9/site-packages/_distutils_hack/../:
pip-23.2.1.dist-info/METADATA
setuptools-68.2.2.dist-info/METADATA

Version

Development (host) and target OS/architectures:

Output of bazel --version: bazel 6.5.0

Version of the Aspect rules, or other relevant rules from your WORKSPACE or MODULE.bazel file: rules_py-0.8.2

Language(s) and/or frameworks involved: python3.9

How to reproduce

py_binary(
    name = "binary",
    srcs = [
        "__init__.py",
    ],
    main = "__init__.py",
    visibility = ["//visibility:public"],
)

py_pex_binary(
    name = "py_pex_binary",
    binary = ":binary",
)


### Any other information?

_No response_

harshalparekh6 avatar Oct 07 '24 05:10 harshalparekh6

Is there a temporal workaround to this issue? I am not sure how it's originated, but would love to create a PEX if there's a workaround.

JoseLGonza avatar Apr 30 '25 17:04 JoseLGonza

I also ran into this. I've found that patching py/private/py_pex_binary.bzl to also exclude my custom Python runtimes in the same way as the common rules_python ones silences the failure. My patch is attached, but your toolchains' naming convention may differ.

aspect_rules_py.pex.iss408.patch

plobsing avatar Jul 29 '25 15:07 plobsing