When Pants packages python dependencies (specifica...
# general
c
When Pants packages python dependencies (specifically using
python_awslambda
target) Is there a difference from importing from a modules init vs using a relative import? For example, lets say I have a utils module that I need a single function from. It's listed in utils
__init__.py
so I can just import it like
from utils import helper_function
. But my problem is that the
__init__.py
also imports some other large helper functions with dependencies that I absolutely don't need. (maybe
other_helper_function
requires pandas) I tried to import
helper_function
as a relative import
from ..utils.helper_file import helper_function
but my
python_awslambda
zip file will include
other_helper_function
Is there any way to only import
helper_function
into the zip? Or do I need to explicitly exclude
other_helper_function
in my BUILD file?
b
Can you remove the reexport of the large function from the init file? Potentially even make it empty If not, you’ll need to do some sort of manual exclusion, but you’ll also need to carefully adjust when the imports happen to avoid failing at runtime with import errors, because the relevant files are being loaded at runtime
c
I can empty out the init file, just forces me to update everywhere that imports any of those functions. It's probably worth the effort though
b
Yeah, I’ve personally come to the conclusion that I should just tolerate large numbers of imports from long-ish module paths (relying on editor support to make this easier), in order to better leverage pants dependency inference, which is all file based. It’s not great “locally” for any individual bit of code, but it’s definitely worth it “globally” for the result on the final packages
c
We mostly do that anyways. Just have a few straggling init files with stuff dumped in there.
👍 1
Thanks for the response! And awesome work on the lambda features. Been playing with 2.18 so I can use the layer packaging. Loving it so far
🎉 1