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 os
print()
print(os.environ)
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 os
import pprint
# User's environment variables
u_env_var = os.environ
print("User's Environment variable:")
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 os
host_name = os.environ['HOSTNAME']
print("HOSTNAME:", host_name)
python_path = os.environ.get('PYTHONPATH')
print("Python Path:", python_path)
Sample Output:
HOSTNAME: 4735090b6baa Python Path: /trinket/python3
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
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.
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework