Python: Get the users environment
Python Basic: Exercise-105 with Solution
Write a Python program to get the users environment.
Sample Solution-1:
Python Code:
# Import the 'os' module for operating system-related functions.
import os
# Print a newline for clarity.
print()
# Access and print the environment variables as a dictionary.
print(os.environ)
# Print another newline for clarity.
print()
Sample Output:
environ({'SHLVL': '3', 'COMP_WORDBREAKS': ' \t\n"\'><;|&(:', 'LESSC LOSE': '/usr/bin/lesspipe %s %s', 'TERM': 'xterm', 'LS_COLORS': 'rs =0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:c d=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow =34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj= 01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31: *.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01; 31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01; 31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz= 01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:* .sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=0 1;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.g if=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;3 5:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.sv g=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;3 5:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.m p4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;3 5:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi ------- PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bi n:/usr/games:/usr/local/games', 'SSH_TTY': '/dev/pts/0', '_': '/usr /bin/timeout'})
Sample Solution-2:
Python Code:
# Import the 'os' module for operating system-related functions.
import os
# Import the 'pprint' module for pretty-printing data structures.
import pprint
# Access and store the user's environment variables.
u_env_var = os.environ
# Print a message indicating the user's environment variables.
print("User's Environment variable:")
# Pretty-print the environment variables in a more readable format.
pprint.pprint(dict(u_env_var), width=1)
Sample Output:
User's Environment variable: {'DEBIAN_FRONTEND': 'teletype', 'HOME': '/root', 'HOSTNAME': '0c299cb8f897', 'LANG': 'en_US.UTF-8', 'LANGUAGE': 'en_US:en', 'LC_ALL': 'en_US.UTF-8', 'MPLBACKEND': 'module://trinket_backend', 'NODE_APP_INSTANCE': '0', 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', 'PM2_DISCRETE_MODE': 'true', 'PM2_HOME': '/root/.pm2', 'PM2_INTERACTOR_PROCESSING': 'true', 'PM2_PROGRAMMATIC': 'true', 'PYTHONPATH': '/trinket/python3', 'TERM': 'xterm', 'automation': 'true', 'autorestart': 'true', 'axm_actions': '', 'axm_dynamic': '[object ' 'Object]', 'axm_monitor': '[object ' 'Object]', 'axm_options': '[object ' 'Object]', 'created_at': '1621962710174', 'env': '[object ' 'Object]', 'exec_interpreter': 'node', 'exec_mode': 'fork_mode', 'exit_code': '0', 'instance_var': 'NODE_APP_INSTANCE', 'instances': '1', 'kill_retry_time': '100', 'km_link': 'false', 'merge_logs': 'true', 'name': 'shell', 'node_args': '', 'node_version': '8.11.3', 'pm_cwd': '/trinket-workdir', 'pm_err_log_path': '/dev/null', 'pm_exec_path': '/trinket-workdir/server/python3/shell.js', 'pm_id': '0', 'pm_out_log_path': '/dev/null', 'pm_pid_path': '/root/.pm2/pids/shell-0.pid', 'pm_uptime': '1622796325496', 'pmx': 'true', 'restart_time': '28', 'status': 'launching', 'treekill': 'true', 'unique_id': '27a8b1a2-c622-4bdb-8745-f55efe2dac0a', 'unstable_restarts': '0', 'username': 'root', 'version': '0.0.0', 'versioning': 'null', 'vizion': 'true', 'vizion_running': 'false', 'windowsHide': 'true'}
Accessing a specific environment variable:
Python Code:
# Import the 'os' module for accessing environment variables.
import os
# Retrieve the 'HOSTNAME' environment variable.
host_name = os.environ['HOSTNAME']
# Print the value of the 'HOSTNAME' environment variable.
print("HOSTNAME:", host_name)
# Retrieve the 'PYTHONPATH' environment variable.
python_path = os.environ.get('PYTHONPATH')
# Print the value of the 'PYTHONPATH' environment variable.
print("Python Path:", python_path)
Sample Output:
HOSTNAME: 4735090b6baa Python Path: /trinket/python3
Python Code Editor:
Previous: Write a Python program to get the effective group id, effective user id, real group id, a list of supplemental group ids associated with the current process.
Next: Write a Python program to divide a path on the extension separator.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/python-basic-exercise-105.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics