-
-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for importing from PEXes. #1845
Conversation
Previously you had to supply a top-level list of modules and packages to enable import redirection for in the directory; now `""` or `"."` indicates redirect for evrything in the directory.
You can now use a PEX as a mostly normal `sys.path` entry. You do need to activate it though and that can be done in two ways: 1. Perform an `import __pex__` before importing anything else you might need that lives in a PEX `sys.path` entry. 2. Import what you need in one step via `from __pex__ import colors` or `from __pex__.colors import red` in the case where the PEX contains ansicolors, for example. The second method is useful in cases where you can't influence which python interpreter to run (In which case you could just specify the PEX file itself), but you can influence the `sys.path`. The motivating example of this is AWS Lambda functions for Python. One mode of deploying these is in a zip. Here you can't pick the Python interpreter, just its version, but you can specify the zip and an entry point. As such you can specify a PEX as the zip and a `__pex__` psuedo-package pre-fixed entry-point. This should obviate the need for projects like Lambdex or at least simplify their integration with Pex, which currently relies on using the un-supported pex.pex_bootstrapper APIs.
@benjyw I think this means we can delete the Pants |
@rtkjliviero here's the promised change that hopefully satisfies #747. |
The 1 failing integration test on the pypy3.9 shard is reproducible, but apparently not related to this change; I can repro on main. CI on main is green though; so stumped for now. |
This was broken from the get-go in pex-tool#1787. It's unclear to me how the `test_create_universal_platform_check` test added in pex-tool#1824 didn't fail from the get-go, but it does now and this fix fixes that test for the `--platform macosx_10.9_x86_64-cp-310-cp310 psutil==5.9.1` abbreviated platform case. Discovered in CI for pex-tool#1845 as a failure unrelated to that change.
…it' into __pex_boot__/magic-vendor-ep
OK, the 1 failing test was in fact unrelated. Fixed in #1846 which is merge in here now for a green CI. |
This was broken from the get-go in #1787. It's unclear to me how the `test_create_universal_platform_check` test added in #1824 didn't fail from the get-go, but it does now and this fix fixes that test for the `--platform macosx_10.9_x86_64-cp-310-cp310 psutil==5.9.1` abbreviated platform case. Discovered in CI for #1845 as a failure unrelated to that change.
That's very neat! Before we drop the backends though, there's one thing I've been meaning to look into for a while, which is whether there are performance optimizations that AWS applies to "standard" lambda zip files that we defeat by using pex (via lambdex or this new way). |
If Lambdexed PEXes are slower for some strange reason then PEXes used this way will be exactly as slow; so perf should have 0 bearing on dropping the Pants backends. If PEX based lambdas are slower and we want to improve that we'd need new backends right? Or are you suggesting exactly that, you'd like to keep the backends / targets named like they are but completely re-work their internals if PEXes are slow? Either way, I need this API use of the bootstrap code gone for Pex 3. |
Thanks @benjyw for taking a look. |
Yeah, I was thinking that depending on the outcome of a performance investigation we might have to reimplement those backends to create "proper" lambda zipfiles, with the layout AWS recommends. |
But that would be outside of PEX entirely, so yeah, I don't think it blocks getting rid of that API. |
You can now use a PEX as a mostly normal
sys.path
entry. You do needto activate it though and that can be done in two ways:
import __pex__
before importing anything else youmight need that lives in a PEX
sys.path
entry.from __pex__ import colors
inthe case where the PEX contains
ansicolors
, for example.The second method is useful in cases where you can't influence which
python interpreter to run (In which case you could just specify the PEX
file itself), but you can influence the
sys.path
. The motivatingexample of this is AWS Lambda functions for Python. One mode of
deploying these is in a zip. Here you can't pick the Python interpreter,
just its version, but you can specify the zip and an entry point. As
such you can specify a PEX as the zip and a
__pex__
psuedo-packageprefixed entry-point. This should obviate the need for projects like
Lambdex or at least simplify their integration with Pex, which currently
relies on using the unsupported
pex.pex_bootstrapper
APIs which I'dlike to drop in Pex 3.
Fixes #1839