Hey yall, hopefully easy question for you :slightl...
# general
p
Hey yall, hopefully easy question for you 🙂 I have a situation where we need to include a loose file in an AWS Lambda function, and reference it by path. But with how we have it working right now, the file path is relative to the current working directory This makes it so the paths are different based on if we are running from the lambda, or the root of our repo. How can I reference a path relative to the pex root?
b
There’s a few options: • make sure it’s within a package and use
importlib.resources
. • use
__file__
to reference the location of the current Python file and then traverse from there The first option is more resilient to weirder and wackier deploy options, but that might not be necessary when you’re in control of the deploy like you seem to be (ie not publishing a library to PyPI)
p
Thanks! Honestly not sure how to use importlib.resources currently, and googling isnt helping much 😅 But I think
__file__
will work! We arent publishing any packages, and its all internal stuff. So even if its less resilient, I think itll work. Thanks for your response!
👍 1
b
Yeah,
importlib.resources
takes a bit of getting used to. I would offer code examples but I’m on a phone... As you say,
__file__
seems good for your use case
Also, as a just-in-case warning, you’ll need to make sure you load the file in pants as a
resources(...)
target, not
files(…)
, or else it won’t be included in the zip
p
Gotcha! Thanks for the heads up, actually ran into that earlier when testing lol