Come recuperare le variabili di ambiente in Mac OS X utilizzando Python?
-
24-10-2019 - |
Domanda
Come posso occhiata variabili d'ambiente (ad esempio l'esportazione hg_user da .profile) utilizzando il codice python in Mac OS X?
Soluzione
os.environ
is a dictionary containing all the environment variables. You need to import os
before you can use it.
So, for example HG_USER would be accessed by os.environ['HG_USER']
.
Altri suggerimenti
Use the os
module:
import os
os.environ
Returns a dictionary with the environment variables as keys.
Use os
:
>>> import os
>>> print os.environ["PATH"]
You can also the helper function os.getenv(varname[, value])
which will return the value of environment variable varname
, and if it not present will return value
which defaults to None
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow