all Python 3osfunction
os.putenv
os — environment
Set an environment variable at the OS level (for child processes).
Syntax
os.putenv(key, value)
It does not update os.environ: after putenv('X', '1') the expression 'X' in os.environ is false. So os.environ['X'] = '1' is almost always better — it calls putenv itself and keeps things consistent. Child processes started after the call see the change. On macOS and FreeBSD repeatedly setting variables may leak memory.
Examples
import os
os.putenv('GLOSSARY_DEMO', '42') # → значение ушло в окружение ОС: его унаследуют дочерние процессы
print(os.environ.get('GLOSSARY_DEMO')) # → None (putenv не обновляет словарь os.environ)
os.environ['GLOSSARY_DEMO'] = '42' # → штатный способ: присваивание в os.environ само вызывает putenv
print(os.getenv('GLOSSARY_DEMO')) # → 42
os.putenv('GLOSSARY_DEMO', 42) # → TypeError: значение обязано быть str или bytes, а не int
See also
Open in the full glossarydocs.python.orgReport an inaccuracy