In this article, we will see a how to print environment variables in Python.
We cab use environ attribute of os module for accessing environment variables using python program. This attribute gives environment dictionary which contains environment variables and its corresponding values.
Now, Let’s see given below Python code:
1 2 3 4 5 6 7 |
# import os module import os # print environment dictionary print(os.environ) |
Output:
1 2 3 |
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', 'APPDATA': 'C:\\Users\\hp\\AppData\\Roaming', 'COMMONPROGRAMFILES': 'C:\\Program Files\\Common Files', 'COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\Common Files', 'COMMONPROGRAMW6432': 'C:\\Program Files\\Common Files', 'COMPUTERNAME': 'LAPTOP-UC56IDQU', 'COMSPEC': 'C:\\windows\\system32\\cmd.exe', 'DRIVERDATA': 'C:\\Windows\\System32\\Drivers\\DriverData', 'HOMEDRIVE': 'C:', 'HOMEPATH': '\\Users\\hp', 'LOCALAPPDATA': 'C:\\Users\\hp\\AppData\\Local', 'LOGONSERVER': '\\\\LAPTOP-UC56IDQU', 'NUMBER_OF_PROCESSORS': '8', 'ONEDRIVE': 'C:\\Users\\hp\\OneDrive', 'ONEDRIVECONSUMER': 'C:\\Users\\hp\\OneDrive', 'ONLINESERVICES': 'Online Services', 'OS': 'Windows_NT', 'PATH': 'C:\\Users\\hp\\Anaconda3;C:\\Users\\hp\\Anaconda3\\Library\\mingw-w64\\bin;C:\\Users\\hp\\Anaconda3\\Library\\usr\\bin;C:\\Users\\hp\\Anaconda3\\Library\\bin;C:\\Users\\hp\\Anaconda3\\Scripts;C:\\Users\\hp\\Anaconda3\\Library\\bin;C:\\windows\\system32;C:\\windows;C:\\windows\\System32\\Wbem;C:\\windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\windows\\System32\\OpenSSH\\;C:\\Program Files\\Microsoft VS Code\\bin;C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python36\\Scripts\\;C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python36\\;C:\\Users\\hp\\AppData\\Local\\Microsoft\\WindowsApps;', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'PLATFORMCODE': 'KV', 'PROCESSOR_ARCHITECTURE': 'AMD64', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 126 Stepping 5, GenuineIntel', 'PROCESSOR_LEVEL': '6', 'PROCESSOR_REVISION': '7e05', 'PROGRAMDATA': 'C:\\ProgramData', 'PROGRAMFILES': 'C:\\Program Files', 'PROGRAMFILES(X86)': 'C:\\Program Files (x86)', 'PROGRAMW6432': 'C:\\Program Files', 'PSMODULEPATH': 'C:\\Program Files\\WindowsPowerShell\\Modules;C:\\windows\\system32\\WindowsPowerShell\\v1.0\\Modules', 'PUBLIC': 'C:\\Users\\Public', 'REGIONCODE': 'APJ', 'SESSIONNAME': 'Console', 'SYSTEMDRIVE': 'C:', 'SYSTEMROOT': 'C:\\windows', 'TEMP': 'C:\\Users\\hp\\AppData\\Local\\Temp', 'TMP': 'C:\\Users\\hp\\AppData\\Local\\Temp', 'USERDOMAIN': 'LAPTOP-UC56IDQU', 'USERDOMAIN_ROAMINGPROFILE': 'LAPTOP-UC56IDQU', 'USERNAME': 'hp', 'USERPROFILE': 'C:\\Users\\hp', 'WINDIR': 'C:\\windows', 'CONDA_PREFIX': 'C:\\Users\\hp\\Anaconda3', 'JPY_INTERRUPT_EVENT': '1956', 'IPY_INTERRUPT_EVENT': '1956', 'JPY_PARENT_PID': '1912', 'TERM': 'xterm-color', 'CLICOLOR': '1', 'PAGER': 'cat', 'GIT_PAGER': 'cat', 'MPLBACKEND': 'module://ipykernel.pylab.backend_inline'}) |
Now, let’s print only environment variables, so for this, we need only keys of environment dictionary.
Below is the Python code given:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# import os module import os # store environment dictionary environment = os.environ print("Environment variables are:") # print environment variables only # keys() method of dictionary # return sequence of keys. for key in environment.keys() : print(key) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
Environment variables are: ALLUSERSPROFILE APPDATA COMMONPROGRAMFILES COMMONPROGRAMFILES(X86) COMMONPROGRAMW6432 COMPUTERNAME COMSPEC DRIVERDATA HOMEDRIVE HOMEPATH LOCALAPPDATA LOGONSERVER NUMBER_OF_PROCESSORS ONEDRIVE ONEDRIVECONSUMER ONLINESERVICES OS PATH PATHEXT PLATFORMCODE PROCESSOR_ARCHITECTURE PROCESSOR_IDENTIFIER PROCESSOR_LEVEL PROCESSOR_REVISION PROGRAMDATA PROGRAMFILES PROGRAMFILES(X86) PROGRAMW6432 PSMODULEPATH PUBLIC REGIONCODE SESSIONNAME SYSTEMDRIVE SYSTEMROOT TEMP TMP USERDOMAIN USERDOMAIN_ROAMINGPROFILE USERNAME USERPROFILE WINDIR CONDA_PREFIX JPY_INTERRUPT_EVENT IPY_INTERRUPT_EVENT JPY_PARENT_PID TERM CLICOLOR PAGER GIT_PAGER MPLBACKEND |
That’s all about how to print environment variables in Python
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.