-
-
Notifications
You must be signed in to change notification settings - Fork 644
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
Running Python code in experimental_shell_command
requires building a PEX and specifying an appropriate interpreter as a tool
#17405
Comments
TO clarify, what's the use-case for running python code via |
We have API servers written in Python, and the code (implicitly) defines the schema for the APIs, and the libraries we use (FastAPI for REST and Strawberry for GraphQL) can generate a static schema file (e.g. OpenAPI for REST). These schemas are then used to generate clients. We could have manual scripts to do the codegen, like the below, but it'd be far better for it to all run via pants with each step cacheable etc. ./pants run path/to/schema-generator.py > schema.json
./pants run path/to/client-generator.js < schema.json (The "additional info" section of #17345 has a fleshed out example of this.) A few additional/tangential points:
|
@huonw Hi! Just trying to wrap my head around this one. I think the solutions for the Python case and the JS case are going to be different, just by virtue of the fact that we already have a good working model for caching Python code, and none at all for JS (yet). For the Python case, it looks to me like there's a few things that matter here:
Have I missed anything? Am I right in thinking that the issue with PEX is not that a PEX gets built, it's just that you have to string together these steps manually? |
Okay, let's focus on Python for now. 👍
Yeah, assuming you're explicitly thinking of the interpreter version as a dependency too.
I guess that's an overall desire yeah, but (other than #17345) IME
Yeah in theory, but slightly lower priority for me (writing wrapper scripts is fine for now).
It's a bit of both. There's the inconvenience of doing it manually but also it's slightly annoying to have an explicit PEX that gets built as part of In particular, the PEX file is purely for in-codebase use, whereas
I think that about covers it. Thanks for working through my rambling description 😅 |
You're definitely right about the behaviour of the codegen step vs a With respect to the JS side of things, we're still trying to wrap our heads around what handling |
FWIW This wouldn't be the case if you ran a |
@thejcannon sure, if you can tolerate the 30-60 seconds build time |
Just trying to point out alternatives 😄 |
Thanks for the tip! If we have devs often tripping over Python version issues, I'll strongly consider
(And points 2 and 3 still hit unnecessary builds during |
Yeah I understand better, the direction this ticket is heading is for a per-language equivalent of |
Or potentially plugin fields for |
At the very least, let's all agree |
Concretely, this might look like: experimental_shell_command(
command="$NPM install && $PYTHON do-thing",
npm_version=..,
python_interpreter_constraints=..,
) ... with those fields being added by plugins, and with useful help strings which describe what environment variable to use to reference the binaries, etc. |
#17680 and follow-up work will have some impact on this |
…box for side-effects (#17716) This adds `experimental_run_in_sandbox`, which allows any target implementing `RunFieldSet`/returning `RunRequest` to be run in the sandbox with its execution dependencies. Amongst other things, this saves needing to explicitly declare a `pex_binary` in order to run a `python_source`, however, other languages aren't as complete right now (though we can work on that). Tests to follow once the API is bedded down: right now, this is _highly_ experimental, and mostly piggybacks on `experimental_shell_command` infrastructure. Closes #17405. Co-authored-by: Huon Wilson <wilson.huon@gmail.com> Co-authored-by: Joshua Cannon <joshdcannon@gmail.com>
Describe the bug
When
experimental_shell_command
needs to run Python code from the repo, it seems the best way to do that is to depend on a PEX and then specify a compatible Python version as atool
(andbash
too). This seems... unwieldy, and the second part in particular seems like it's liable to end up with 'works on my machine' problems, if developers have differentPATH
configurations.Example: https://gist.github.com/huonw/47bc63951eac7a05a3a3442843f040a9
git clone https://gist.github.com/47bc63951eac7a05a3a3442843f040a9.git cd 47bc63951eac7a05a3a3442843f040a9 ./pants run //:print
This would be better if (numbers corresponding to
BUG
comments):script.py
(and its venv/other dependencies) directly, similar to how./pants run script.py
works mostly the same as./pants run //:pex
. This would avoid needing another PEX binary that gets unnecessarily explicitly packaged on./pants package ::
.tools
interpreter_constraints = ["CPython==3.7.*"]
(andtools=["python3.7", "bash"]
), running./pants run script.py
works fine, but neither./pants run //:pex
nor./pants run //:print
do.python3.7
hits the pyenv shim, and gets an error likeThe `python3.7' command exists in these Python versions: 3.7.13
).pyenv global 3.7.13
orpyenv shell 3.7.13
first, and then rerunning./pants run //:print
works fine, but would presumably break anything using other versions...Key files for posterity/convenience:
Pants version
2.15.0a0
OS
macOS
Additional info
This is related to https://pantsbuild.slack.com/archives/C046T6T9U/p1666315386420799, and has the same context as #17345: we're using
experimental_shell_command
for ad hoc code generation, where we execute part of our API code to generate a schema file.The text was updated successfully, but these errors were encountered: