spack python: add --path option (#22006)

This adds a `--path` option to `spack python` that shows the `python`
interpreter that Spack is using.

e.g.:

```console
$ spack python --path
/Users/gamblin2/src/spack/var/spack/environments/default/.spack-env/view/bin/python
```

This is useful for debugging, and we can ask users to run it to
understand what python Spack is picking up via preferences in `bin/spack`
and via the `SPACK_PYTHON` environment variable introduced in #21222.
This commit is contained in:
Todd Gamblin 2021-03-07 13:37:26 -08:00 committed by GitHub
parent 7aa5cc241d
commit 8d3272f82d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 1 deletions

View File

@ -33,6 +33,9 @@ def setup_parser(subparser):
subparser.add_argument( subparser.add_argument(
'-m', dest='module', action='store', '-m', dest='module', action='store',
help='run library module as a script') help='run library module as a script')
subparser.add_argument(
'--path', action='store_true', dest='show_path',
help='show path to python interpreter that spack uses')
subparser.add_argument( subparser.add_argument(
'python_args', nargs=argparse.REMAINDER, 'python_args', nargs=argparse.REMAINDER,
help="file to run plus arguments") help="file to run plus arguments")
@ -43,6 +46,10 @@ def python(parser, args, unknown_args):
print('Python', platform.python_version()) print('Python', platform.python_version())
return return
if args.show_path:
print(sys.executable)
return
if args.module: if args.module:
sys.argv = ['spack-python'] + unknown_args + args.python_args sys.argv = ['spack-python'] + unknown_args + args.python_args
runpy.run_module(args.module, run_name="__main__", alter_sys=True) runpy.run_module(args.module, run_name="__main__", alter_sys=True)

View File

@ -4,6 +4,7 @@
# SPDX-License-Identifier: (Apache-2.0 OR MIT) # SPDX-License-Identifier: (Apache-2.0 OR MIT)
import platform import platform
import sys
import pytest import pytest
@ -18,6 +19,11 @@ def test_python():
assert out.strip() == spack.spack_version assert out.strip() == spack.spack_version
def test_python_interpreter_path():
out = python('--path')
assert out.strip() == sys.executable
def test_python_version(): def test_python_version():
out = python('-V') out = python('-V')
assert platform.python_version() in out assert platform.python_version() in out

View File

@ -1385,7 +1385,7 @@ _spack_pydoc() {
_spack_python() { _spack_python() {
if $list_options if $list_options
then then
SPACK_COMPREPLY="-h --help -V --version -c -i -m" SPACK_COMPREPLY="-h --help -V --version -c -i -m --path"
else else
SPACK_COMPREPLY="" SPACK_COMPREPLY=""
fi fi