I have some packages that use `importlib.metadata....
# general
f
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
That sounds like: https://github.com/pantsbuild/pants/discussions/17308 See what you think of that answer.
f
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
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
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
IIRC adding the dependency will install the distribution in your chroot.
f
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
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
OK, I’ll just do a
try/except
in
__init__.py
😄
e
This is all just a very buggy corner still.
f
I don’t need to test the version, so that should be fine
e
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
Yeah, getting the dependency resolution right here is pretty tricky, it seems like