Skip to content

Commit 8d3272f

Browse files
authored
spack python: add --path option (spack#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 spack#21222.
1 parent 7aa5cc2 commit 8d3272f

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

lib/spack/spack/cmd/python.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def setup_parser(subparser):
3333
subparser.add_argument(
3434
'-m', dest='module', action='store',
3535
help='run library module as a script')
36+
subparser.add_argument(
37+
'--path', action='store_true', dest='show_path',
38+
help='show path to python interpreter that spack uses')
3639
subparser.add_argument(
3740
'python_args', nargs=argparse.REMAINDER,
3841
help="file to run plus arguments")
@@ -43,6 +46,10 @@ def python(parser, args, unknown_args):
4346
print('Python', platform.python_version())
4447
return
4548

49+
if args.show_path:
50+
print(sys.executable)
51+
return
52+
4653
if args.module:
4754
sys.argv = ['spack-python'] + unknown_args + args.python_args
4855
runpy.run_module(args.module, run_name="__main__", alter_sys=True)

lib/spack/spack/test/cmd/python.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
55

66
import platform
7+
import sys
78

89
import pytest
910

@@ -18,6 +19,11 @@ def test_python():
1819
assert out.strip() == spack.spack_version
1920

2021

22+
def test_python_interpreter_path():
23+
out = python('--path')
24+
assert out.strip() == sys.executable
25+
26+
2127
def test_python_version():
2228
out = python('-V')
2329
assert platform.python_version() in out

share/spack/spack-completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ _spack_pydoc() {
13851385
_spack_python() {
13861386
if $list_options
13871387
then
1388-
SPACK_COMPREPLY="-h --help -V --version -c -i -m"
1388+
SPACK_COMPREPLY="-h --help -V --version -c -i -m --path"
13891389
else
13901390
SPACK_COMPREPLY=""
13911391
fi

0 commit comments

Comments
 (0)