diff --git a/docs/examples.md b/docs/examples.md index 19c09fa2..199c4fe1 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -185,9 +185,21 @@ print(function()) # This returns your python path (which you be inside your vir Some useful parameters of the `CommandFunction` class: - `cwd`: to choose from which directory the command is run. -- `env`: to provide specific environment variables. +- `env`: to replace the subprocess environment. If omitted, the current environment is inherited. To add variables while keeping `PATH` and other inherited values, merge them with `os.environ`: - `verbose`: set to `False` if you do not want any logging. +```python +import os +import submitit + +function = submitit.helpers.CommandFunction( + ["which", "python"], + env={**os.environ, "MY_JOB_SETTING": "value"}, +) +``` + +Passing only `env={"MY_JOB_SETTING": "value"}` does not preserve the inherited `PATH` or other variables. Use a complete environment when that isolation is intentional. + As an experimental feature, you can also provide arguments when calling the instance: ```python print(submitit.helpers.CommandFunction(["which"])("pip")) # will run "which pip" diff --git a/submitit/core/test_utils.py b/submitit/core/test_utils.py index c5799d9c..f5dc132f 100644 --- a/submitit/core/test_utils.py +++ b/submitit/core/test_utils.py @@ -110,3 +110,23 @@ def test_jobpaths(tmp_path: Path) -> None: utils.JobPaths(tmp_path / "array-%A-index-%a", "456_3").stdout == tmp_path / "array-456-index-3" / "456_3_0_log.out" ) + + +def test_command_function_environment(monkeypatch) -> None: + monkeypatch.setenv("SUBMITIT_TEST_PARENT", "inherited") + monkeypatch.delenv("SUBMITIT_TEST_CHILD", raising=False) + command = [ + sys.executable, + "-c", + "import os; print(os.getenv('SUBMITIT_TEST_PARENT', 'missing'), " + "os.getenv('SUBMITIT_TEST_CHILD', 'missing'))", + ] + assert utils.CommandFunction(command, verbose=False)() == "inherited missing" + replacement = {"SUBMITIT_TEST_CHILD": "value"} + assert utils.CommandFunction( + command, env=replacement, verbose=False + )() == "missing value" + merged = {**os.environ, **replacement} + assert utils.CommandFunction( + command, env=merged, verbose=False + )() == "inherited value"