adding ipython interpreter option for developers#20329
adding ipython interpreter option for developers#20329scheibelp merged 10 commits intospack:developfrom
Conversation
Signed-off-by: vsoch <[email protected]>
Signed-off-by: vsoch <[email protected]>
Signed-off-by: vsoch <[email protected]>
adamjstewart
left a comment
There was a problem hiding this comment.
This is great! I imagine a lot of other users will like this. Since it adds a new feature, let's get at least one more core developer to review. Also pinging some heavy Python users: @trws @skosukhin
scheibelp
left a comment
There was a problem hiding this comment.
I have a few requests relating to default behavior.
Also, is there an IPython executable? I'm wondering if we could get more flexibility by specifying the executable as an argument, modifying e.g. PYTHONPATH, and then launching the executable (vs. importing the relevant interpreter library) - that would allow supporting other interpreter options without modifying Spack.
Yes, it's called |
This would definitely make sense for something that we cannot interact with via Python, but for these interpreters that's not the case so I'm not sure it's warranted yet. Also, we would need to be able to still provide the local environment with spack ready to go, which would be more challenging allowing for the user to specify any old executable. It's a cool idea, but maybe we should consider it when there is evidence that users want to use their own custom interpreter executables? |
|
okay! I've updated the PR to more explicitly exit with an error message for the user when a combination of args is invalid (e.g., a command and anything other than ipython) or if ipython cannot be imported. See a691e9a. |
the client should exit if the user provides an argument/command paired with anything other than python, as it is not really logical. We also exit if ipython is not able to be imported, vs. we previously switched over to python instead and issued a warning. Signed-off-by: vsoch <[email protected]>
|
(EDIT: this was already addressed by #20329 (comment))
This makes sense but are there interpreters other than IPython or the default Python which are available as imports? If not, I think |
|
Yes, all the ones that I mentioned are available to be called interactively. If it's a Python library that gets called from the command line, logically there is a way to bypass the client and do from within Python. Here are a few examples for the ones I mentioned: I think we should take this approach, the simplest one, until there is strong reason (user demand) to do otherwise. |
|
This seems somewhat overfit to ipython to me personally, I think largely because of the fact the |
scheibelp
left a comment
There was a problem hiding this comment.
I think the only thing needed here is updating the documentation in line with the updated error behavior.
lib/spack/docs/developer_guide.rst
Outdated
|
|
||
| just like you would with the normal ``python`` command. | ||
| just like you would with the normal ``python`` command. Note that for both | ||
| commands and files, the default will always be the python interpreter, |
There was a problem hiding this comment.
With new edits, -i ipython will through an error so I think comment can be updated accordingly.
There was a problem hiding this comment.
My bad, totally forgot about updating the docs! I'll do that now.
@trws IMO the documentation added here was clear that only I also felt that this solution wouldn't get in the way of a set- |
to explicitly state that the options are 1) current python (default) or 2) ipython. And show that commands/args must be done with python Signed-off-by: vsoch <[email protected]>
|
I updated the docs to be consistent with the current implementation, and (hopefully) did a better job to address @trws about explicitly stating the two options. 849b4c5. I also used more consistent capitalization for Python and IPython. Strange! GitHub links the commit to the repository here (with a message that the commit doesn't belong) but it should be linked to my fork, vsoch@849b4c5. |
|
It probably won't get in the way. The docs seem to have some remaining issues though, in that |
|
That was a typo, and having the redundancy is probably confusing. Fixed in c4ca219. The very first part of the section does mention that |
Signed-off-by: vsoch <[email protected]>
|
As a possible solution, I just removed the extra "clarification" (which wasn't very good, my bad!) about why we would only allow python for commands and scripts. It now just directly states the options without trying to explain them, which sounds much better I think. |
Signed-off-by: vsoch <[email protected]>
Signed-off-by: vsoch <[email protected]>
|
The doc updates look good. 👍 The thing I'm contemplating now though is that while I occasionally use ipython, I almost never use it interactively. I normally use it to get better errors, or turn on its pdb integration and use it as a debugger, things like this: Why prevent this use? |
Oh, interesting, I've never used it like that before! I typically just embed it everywhere to get an interactive session to develop and debug. @trws if I understand correctly, we should allow using ipython for the script use case (and string arguments as well?) and remove the checks that don't allow it? |
|
If possible? I actually use embedding too, but usually by adding an |
Signed-off-by: vsoch <[email protected]>
|
@trws I gave it a shot! 8fa2064. One thing I'm wondering about is if it should be allowed to run a command and a script. It seems to functionally work for python (and I tested for ipython but it's not included in the PR here) but I'm not sure it's working in practice. For example, let's say I have a script that prints the current working directory, but doesn't have the os import: # script.py
print(os.getcwd())If I run the script with the python interpreter, this is what I'd expect: $ bin/spack python script.py
Traceback (most recent call last):
File "script.py", line 2, in <module>
print(os.getcwd())
NameError: name 'os' is not definedArguably since the console command is run before the script, I should be able to add it, and then see the printing: $ bin/spack python -c "import os" script.py
# nothing printedIt's not an issue of the import working, because I can change to sys (and I'd expect to see the error message again) but I don't: $ bin/spack python -c "import sys" script.py
# nothing printedThe only thing that "works" at least to run and still produce the output is to have the $ bin/spack python script.py -c "import os"
Traceback (most recent call last):
File "script.py", line 2, in <module>
print(os.getcwd())
NameError: name 'os' is not definedBut the import is still not done in context of the script. So if this is a bug, while it's present we should check that both are defined in the parent function and cut out early if this is the case. If the user wants to run a command alongside a script, arguably it's just easiest to write it into the script. Let me know what you think! |
|
Wow, I had not thought of that at all. So, doing a bit of similar testing with python the executable, it seems as though if you specify |
|
So just to clarify, you think that we shouldn’t allow specifying both (the spack -c, not a -c to go to Python) and exit early if this is the case? This is my suggestion because having both doesn’t seem to work! And of course the user is free to test specifying -c after a command so it’s handed off to Python or IPython. |
|
I just pushed a fix 75e5c83 to cut out early if both are specified. So this won't work: $ spack python -c "import sys" script.py
==> Error: You can only specify a command OR script, but not both.But this will, and the user is free to do whatever they like! Likely a separate command could be run first, independent of the script, but not something you expect to hand off to the script like an IPython embed. $ bin/spack python script.py -c "import os"
Traceback (most recent call last):
File "script.py", line 2, in <module>
print(os.getcwd())
NameError: name 'os' is not defined |
Signed-off-by: vsoch <[email protected]>
|
Thanks! |
|
Wooo merged! Thanks y'all! :D |
|
Thank you, good addition, and thanks for working through the feedback. |
This adds a -i option to "spack python" which allows use of the IPython interpreter; it can be used with "spack python -i ipython". This assumes it is available in the Python instance used to run Spack (i.e. that you can "import IPython").
This adds a -i option to "spack python" which allows use of the IPython interpreter; it can be used with "spack python -i ipython". This assumes it is available in the Python instance used to run Spack (i.e. that you can "import IPython").
This is a small pull request to add an option to use an ipython interpreter for developers, which is my preference (and maybe others too?) Basically, you can specify it like this:
I replaced the console.runsource with a simple exec, since embed() essentially includes all the locals anyway. For running commands/files I force the user to use python, because while it works to use ipython, it doesn't make sense for a non-interactive use case.
This is my first PR in a long time so I suspect the formatting will be wrong, I'll follow up with fixes when the CI tells me I'm a wet noodle.