Skip to content
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

Modify PATH on userscripts call to include path to python executable #491

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions payu/fsops.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,18 @@ def needs_subprocess_shell(command: str) -> bool:
return False


def env_with_python_path():
"""Return a copy of current environment variables with PATH
modified to include the python interpreter"""
# Get the directory of the current Python interpreter
python_dir = os.path.dirname(sys.executable)

# Add Python interpreter to PATH
env = os.environ.copy()
env["PATH"] = python_dir + os.pathsep + env["PATH"]
return env


def _run_script(script_cmd: str, control_path: Path) -> None:
"""Helper recursive function to attempt running a script command.

Expand All @@ -315,9 +327,11 @@ def _run_script(script_cmd: str, control_path: Path) -> None:
# First try to interpret the argument as a full command
try:
if needs_subprocess_shell(script_cmd):
subprocess.check_call(script_cmd, shell=True)
subprocess.check_call(script_cmd, shell=True,
env=env_with_python_path())
else:
subprocess.check_call(shlex.split(script_cmd))
subprocess.check_call(shlex.split(script_cmd),
env=env_with_python_path())
print(script_cmd)

except FileNotFoundError:
Expand Down
Loading