Skip to content

Commit e2e2e8e

Browse files
Евгений БлиновЕвгений Блинов
authored andcommitted
Environment better docs
1 parent c84d3e8 commit e2e2e8e

1 file changed

Lines changed: 35 additions & 72 deletions

File tree

README.md

Lines changed: 35 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,41 @@ run('python -c pass', directory=Path.home())
184184

185185
> ⚠️ If the directory is “broken” — for example, if it does not exist, or if there is a file at the specified path instead of a directory — a `suby.errors.WrongDirectoryError` exception will be raised. This exception will not be [caught](#exceptions) if `catch_exceptions=True` is used.
186186
187+
Use `env` when the subprocess should receive exactly the variables you provide:
188+
189+
```python
190+
run(
191+
'python -c "import os; print(os.environ.get(\'MY_VARIABLE\'))"',
192+
env={'MY_VARIABLE': 'hello'},
193+
)
194+
#> hello
195+
```
196+
197+
> ↑ When `env` is provided, variables from the current process are not added automatically. `env={}` means a truly empty environment, which may make some executables fail on platforms that require system variables.
198+
199+
Use `add_env` to start with the current process environment and add or override selected variables:
200+
201+
```python
202+
run(
203+
'python -c "import os; print(os.environ.get(\'MY_VARIABLE\'))"',
204+
add_env={'MY_VARIABLE': 'hello'},
205+
)
206+
#> hello
207+
```
208+
209+
And use `delete_env` to remove variables from the environment passed to the subprocess:
210+
211+
```python
212+
# If MY_VARIABLE exists in the current process environment, it will not be passed to this subprocess.
213+
run(
214+
'python -c "import os; print(os.environ.get(\'MY_VARIABLE\'))"',
215+
delete_env=['MY_VARIABLE'],
216+
)
217+
#> None
218+
```
219+
220+
> ⓘ On `Windows`, environment variable names are handled case-insensitively. On other platforms, names are case-sensitive.
221+
187222

188223
## Output
189224

@@ -390,75 +425,3 @@ Just as with [regular cancellation tokens](#working-with-cancellation-tokens), y
390425
print(run('python -c "import time; time.sleep(10_000)"', timeout=1, catch_exceptions=True))
391426
#> SubprocessResult(id='ea88c518d25011eeb25e320319d7541c', stdout='', stderr='', returncode=-9, killed_by_token=True)
392427
```
393-
394-
395-
## Environment variables
396-
397-
By default, a subprocess receives the same environment variables as the current process:
398-
399-
```python
400-
run('python -c "import os; print(os.environ.get(\'MY_VARIABLE\'))"')
401-
```
402-
403-
Use `env` when the subprocess should receive exactly the variables you provide:
404-
405-
```python
406-
run(
407-
'python -c "import os; print(os.environ.get(\'MY_VARIABLE\'))"',
408-
env={'MY_VARIABLE': 'hello'},
409-
)
410-
#> hello
411-
```
412-
413-
When `env` is provided, variables from the current process are not added automatically. `env={}` means a truly empty environment, which may make some executables fail on platforms that require system variables.
414-
415-
Use `add_env` to start with the current process environment and add or override selected variables:
416-
417-
```python
418-
run(
419-
'python -c "import os; print(os.environ.get(\'MY_VARIABLE\'))"',
420-
add_env={'MY_VARIABLE': 'hello'},
421-
)
422-
#> hello
423-
```
424-
425-
If both `env` and `add_env` are provided, `add_env` is applied after `env`:
426-
427-
```python
428-
run(
429-
'python -c "import os; print(os.environ.get(\'MY_VARIABLE\'))"',
430-
env={'MY_VARIABLE': 'from env'},
431-
add_env={'MY_VARIABLE': 'from add_env'},
432-
)
433-
#> from add_env
434-
```
435-
436-
Use `delete_env` to remove variables from the environment passed to the subprocess:
437-
438-
```python
439-
# If MY_VARIABLE exists in the current process environment, it will not be
440-
# passed to this subprocess.
441-
run(
442-
'python -c "import os; print(os.environ.get(\'MY_VARIABLE\'))"',
443-
delete_env=['MY_VARIABLE'],
444-
)
445-
#> None
446-
```
447-
448-
`delete_env` is applied last. A variable cannot be explicitly set through `env` or `add_env` and deleted through `delete_env` in the same call:
449-
450-
```python
451-
from suby import EnvironmentVariablesConflict
452-
453-
try:
454-
run(
455-
'python -c pass',
456-
env={'MY_VARIABLE': 'hello'},
457-
delete_env=['MY_VARIABLE'],
458-
)
459-
except EnvironmentVariablesConflict as error:
460-
print(error)
461-
#> Environment variables cannot be both set via env/add_env and deleted via delete_env: MY_VARIABLE.
462-
```
463-
464-
On `Windows`, environment variable names are handled case-insensitively. On other platforms, names are case-sensitive.

0 commit comments

Comments
 (0)