early-twilight-26336
06/20/2024, 8:46 PMpkgutil.iter_modules
doesn't work as expected when an application is packaged as a PEX? Gives an empty array, but it works when run in a different environment. I verified that the modules in question are present in the PEX.early-twilight-26336
06/20/2024, 9:09 PMpants package <target>
and then just
cd dist/<target>/bin.pex
python __main__.py
everything works as expected.
but, doing pants run <target>
does not.early-twilight-26336
06/20/2024, 9:10 PMlayout="loose"
to get the PEX in the dir format I can cd
intoearly-twilight-26336
06/20/2024, 9:26 PMpkgutil.iter_modules
finds it. So seems like Pants/PEX thinks the module isn't needed because it's never explicitly imported, and so the PEX doesn't include it in the right way somehow.
the whole point of the pkgutil.iter_modules
is to do dynamic imports. I guess I need to do some manual dependency specification somewhere...fast-nail-55400
06/20/2024, 10:23 PMearly-twilight-26336
06/20/2024, 10:34 PMfast-nail-55400
06/20/2024, 11:01 PMBUILD
files.fast-nail-55400
06/20/2024, 11:03 PMdependencies
field on a target)fast-nail-55400
06/20/2024, 11:03 PMearly-twilight-26336
06/21/2024, 12:24 AMpython_sources(name="thing", sources=["app/**/*.py"]) # contains the modules that are dynamically imported
pex_binary(
name="bin",
dependencies=[":thing"],
execution_mode="venv",
layout="loose",
script="bentoml",
args=["serve", "app.service:svc"]
)
early-twilight-26336
06/21/2024, 12:27 AMpython_source
and adding the imported modules as dependencies of thatearly-twilight-26336
06/21/2024, 12:29 AMpants dependencies thing:bin
the dynamically imported modules show up in the list...early-twilight-26336
06/21/2024, 2:44 AMpkgutil.iter_modules
accepts a path, not a module, and the path was different inside the PEX. Doing this makes it work everywhere: https://stackoverflow.com/a/1310912early-twilight-26336
06/21/2024, 2:59 AM