Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 13 additions & 1 deletion docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 20 additions & 0 deletions submitit/core/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"