https://pantsbuild.org/ logo
#general
Title
# general
f

future-oxygen-10553

10/26/2022, 7:14 PM
I have some packages that use
importlib.metadata.version
to get the installed version of my packages at runtime from the installation metadata. Unfortunately, pants doesn’t install packages into its hermetic environment when the package is under test, so importing the package fails because
importlib.metadata
cannot find the package. Is there any way around this other than
try/except
in the
__init__.py
for the package?
e

enough-analyst-54434

10/26/2022, 7:35 PM
That sounds like: https://github.com/pantsbuild/pants/discussions/17308 See what you think of that answer.
f

future-oxygen-10553

10/26/2022, 7:53 PM
I don’t think that’ll work for the version? I think I’d need to add
my-package.dist-info/METADATA
, but that has a lot more than just the name and version in it
e

enough-analyst-54434

10/26/2022, 7:57 PM
Right. So have you tried adding an explicit dependency on your
python_distribution
target(s) to tests? IIUC that should work. May be not fun, but easy to try for 1 dist + 1 test file anyhow.
f

future-oxygen-10553

10/26/2022, 8:00 PM
So if I follow @happy-kitchen-89482’s solution and copy the
METADATA
file out of the built wheel, then add a dependency for my tests on that
dist-info
, that works. Let me try adding a dependency on the distribution
e

enough-analyst-54434

10/26/2022, 8:01 PM
IIRC adding the dependency will install the distribution in your chroot.
f

future-oxygen-10553

10/26/2022, 8:02 PM
I think it tried, but I got an error about missing dependencies
Copy code
pex.environment.ResolveError: Failed to resolve requirements from PEX environment @ /Users/bweber/.cache/pants/named_caches/pex_root/unzipped_pexes/6a3825aa222710ffabd3d0bafb0d95ce6f002e0e.
Needed cp39-cp39-macosx_12_0_arm64 compatible dependencies for:
 1: scikit-learn>=1.1.2
    Required by:
      chuck_norris 0.0.2
    But this pex had no ProjectName(raw='scikit-learn', normalized='scikit-learn') distributions.
e

enough-analyst-54434

10/26/2022, 8:02 PM
This all gets messy in a hurry since now your code is represented 2x in the chroot, once from the loose source dependencies Pants infers, and once from the built wheel.
Exactly!
That is dup 3rdparty.
The dup 1st party was fixed IIRC.
f

future-oxygen-10553

10/26/2022, 8:02 PM
OK, I’ll just do a
try/except
in
__init__.py
😄
e

enough-analyst-54434

10/26/2022, 8:02 PM
This is all just a very buggy corner still.
f

future-oxygen-10553

10/26/2022, 8:02 PM
I don’t need to test the version, so that should be fine
e

enough-analyst-54434

10/26/2022, 8:05 PM
FWIW, the issue that spawned the discussion question is still open unlike the question and my comment here replicates your woe: https://github.com/pantsbuild/pants/issues/11386#issuecomment-1136530162
f

future-oxygen-10553

10/26/2022, 8:07 PM
Yeah, getting the dependency resolution right here is pretty tricky, it seems like
8 Views