summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMassimiliano Culpo <massimiliano.culpo@gmail.com>2020-01-25 01:49:45 +0100
committerTodd Gamblin <tgamblin@llnl.gov>2020-01-24 16:49:45 -0800
commit4d7d65736687cb03ac3c9b6ca685dca86935227a (patch)
tree81adbfb32b9d6de1054440159969c19aae9622ba /lib
parentd646c8d8d5bf4cf5ddcb4ac04ce29efa5a8cd2df (diff)
downloadspack-4d7d65736687cb03ac3c9b6ca685dca86935227a.tar.gz
spack-4d7d65736687cb03ac3c9b6ca685dca86935227a.tar.bz2
spack-4d7d65736687cb03ac3c9b6ca685dca86935227a.tar.xz
spack-4d7d65736687cb03ac3c9b6ca685dca86935227a.zip
bugfix: make `_source_single_file` work in venvs (#14569)
Using `sys.executable` to run Python in a sub-shell doesn't always work in a virtual environment as the `sys.executable` Python is not necessarily compatible with any loaded spack/other virtual environment. - revert use of sys.executable to print out subshell environment (#14496) - try instead to use an available python, then if there *is not* one, use `sys.executable` - this addresses RHEL8 (where there is no `python` and `PYTHONHOME` issue in a simpler way
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/util/environment.py11
-rw-r--r--lib/spack/spack/util/module_cmd.py5
2 files changed, 10 insertions, 6 deletions
diff --git a/lib/spack/spack/util/environment.py b/lib/spack/spack/util/environment.py
index 18a365dcef..248a6d3c8c 100644
--- a/lib/spack/spack/util/environment.py
+++ b/lib/spack/spack/util/environment.py
@@ -17,7 +17,6 @@ import six
import llnl.util.tty as tty
import spack.util.executable as executable
-from spack.util.module_cmd import py_cmd
from llnl.util.lang import dedupe
@@ -919,8 +918,14 @@ def environment_after_sourcing_files(*files, **kwargs):
source_file.extend(x for x in file_and_args)
source_file = ' '.join(source_file)
- dump_environment = 'PYTHONHOME="{0}" "{1}" -c "{2}"'.format(
- sys.prefix, sys.executable, py_cmd)
+ # If the environment contains 'python' use it, if not
+ # go with sys.executable. Below we just need a working
+ # Python interpreter, not necessarily sys.executable.
+ python_cmd = executable.which('python3', 'python', 'python2')
+ python_cmd = python_cmd.name if python_cmd else sys.executable
+
+ dump_cmd = 'import os, json; print(json.dumps(dict(os.environ)))'
+ dump_environment = python_cmd + ' -c "{0}"'.format(dump_cmd)
# Try to source the file
source_file_arguments = ' '.join([
diff --git a/lib/spack/spack/util/module_cmd.py b/lib/spack/spack/util/module_cmd.py
index f7716854c7..0edf7e6102 100644
--- a/lib/spack/spack/util/module_cmd.py
+++ b/lib/spack/spack/util/module_cmd.py
@@ -18,7 +18,7 @@ import llnl.util.tty as tty
# This list is not exhaustive. Currently we only use load and unload
# If we need another option that changes the environment, add it here.
module_change_commands = ['load', 'swap', 'unload', 'purge', 'use', 'unuse']
-py_cmd = 'import os; import json; print(json.dumps(dict(os.environ)))'
+py_cmd = "'import os;import json;print(json.dumps(dict(os.environ)))'"
# This is just to enable testing. I hate it but we can't find a better way
_test_mode = False
@@ -32,8 +32,7 @@ def module(*args):
if args[0] in module_change_commands:
# Do the module manipulation, then output the environment in JSON
# and read the JSON back in the parent process to update os.environ
- module_cmd += ' > /dev/null; PYTHONHOME="{0}" "{1}" -c "{2}"'.format(
- sys.prefix, sys.executable, py_cmd)
+ module_cmd += ' >/dev/null;' + sys.executable + ' -c %s' % py_cmd
module_p = subprocess.Popen(module_cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,