Logging Environment Details with Python
Source: Notion | Last edited: 2024-09-26 | ID: 577bf4ce-e83...
This tutorial will guide you through creating and running a Python script that logs various environment details, including Python environment, system details, Git repository information, and common environment variables. The script will also provide explanations for each section of the Git output to help you better understand the information being displayed.
Step 1: Copy and Paste the Script
Section titled “Step 1: Copy and Paste the Script”Copy the following script and paste it into your terminal. This script will log the environment details and provide explanations for each section of the Git output.
# Create and run the temporary Python scriptcat << 'EOF' > /tmp/log_env_details.pyimport sysimport osimport platformimport subprocess
def print_header(title): print(f"\\n{'-'*50}") print(f"{title:^50}") print(f"{'-'*50}")
def print_detail(key, value, explanation=None): print(f"{key}:") if explanation: print(f" {explanation}") if isinstance(value, list): for item in value: print(f" - {item}") elif isinstance(value, str) and key != "Remote URL" and ':' in value: for item in value.split(':'): print(f" - {item}") else: print(f" {value}")
def log_environment_details(): print_header("Python Environment Details")
print_detail("sys.prefix", sys.prefix) print_detail("sys.executable", sys.executable) print_detail("sys.path", sys.path) print_detail("PYTHONPATH", os.environ.get("PYTHONPATH", "Not set")) print_detail("VIRTUAL_ENV", os.environ.get("VIRTUAL_ENV", "Not set"))
def log_system_details(): print_header("System Details")
print_detail("Platform", platform.system()) print_detail("Platform Version", platform.version()) print_detail("Platform Release", platform.release()) print_detail("Machine", platform.machine()) print_detail("Processor", platform.processor()) print_detail("Python Version", platform.python_version())
def log_git_details(): print_header("Git Details")
try: branch = subprocess.check_output(["git", "branch", "--show-current"]).strip().decode() print_detail("Current Branch", branch, "The current branch you are working on.") except subprocess.CalledProcessError: print_detail("Current Branch", "Not a git repository")
try: branch_alt = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).strip().decode() print_detail("Current Branch (alt)", branch_alt, "Alternative method to get the current branch.") except subprocess.CalledProcessError: print_detail("Current Branch (alt)", "Not a git repository")
try: remote_url = subprocess.check_output(["git", "config", "--get", "remote.origin.url"]).strip().decode() print_detail("Remote URL", remote_url, "The URL of the remote repository named 'origin'.") except subprocess.CalledProcessError: print_detail("Remote URL", "Not a git repository")
try: latest_commit = subprocess.check_output(["git", "log", "-1", "--pretty=format:%H - %s"]).strip().decode() print_detail("Latest Commit", latest_commit, "The latest commit hash and message.") except subprocess.CalledProcessError: print_detail("Latest Commit", "Not a git repository")
try: status = subprocess.check_output(["git", "status", "--short"]).strip().decode().split('\\n') print_detail("Status", status if status != [''] else "Clean", "The status of the working directory.") except subprocess.CalledProcessError: print_detail("Status", "Not a git repository")
try: remotes = subprocess.check_output(["git", "remote", "-v"]).strip().decode().split('\\n') print_detail("Remotes", remotes, "The configured remote repositories and their URLs.") except subprocess.CalledProcessError: print_detail("Remotes", "Not a git repository")
def log_env_variables(): print_header("Environment Variables")
env_vars = ["HOME", "USER", "SHELL", "PATH", "LANG", "TERM"] for var in env_vars: print_detail(var, os.environ.get(var, "Not set"))
if __name__ == "__main__": log_environment_details() log_system_details() log_git_details() log_env_variables()EOF
python /tmp/log_env_details.py
# Clean up by removing the temporary Python scriptrm /tmp/log_env_details.pyStep 2: Run the Script
Section titled “Step 2: Run the Script”After pasting the script into your terminal, press Enter to run it. The script will log the environment details and provide explanations for each section of the Git output.
Example Output
Section titled “Example Output”Here’s an example of what the output might look like:
-------------------------------------------------- Python Environment Details--------------------------------------------------sys.prefix: /Users/terryli/.pyenv/versions/eon-3.12.5sys.executable: /Users/terryli/.pyenv/versions/eon-3.12.5/bin/pythonsys.path: - /private/tmp - /Users/terryli/.pyenv/versions/3.12.5/lib/python312.zip - /Users/terryli/.pyenv/versions/3.12.5/lib/python3.12 - /Users/terryli/.pyenv/versions/3.12.5/lib/python3.12/lib-dynload - /Users/terryli/.pyenv/versions/eon-3.12.5/lib/python3.12/site-packagesPYTHONPATH: Not setVIRTUAL_ENV: /Users/terryli/.pyenv/versions/3.12.5/envs/eon-3.12.5
-------------------------------------------------- System Details--------------------------------------------------Platform: DarwinPlatform Version: Darwin Kernel Version 24.0.0: Mon Aug 12 21:29:10 PDT 2024; root:xnu-11215.1.10~5/RELEASE_ARM64_T6031Platform Release: 24.0.0Machine: arm64Processor: armPython Version: 3.12.5
-------------------------------------------------- Git Details--------------------------------------------------Current Branch: megaphone_v1 The current branch you are working on.Current Branch (alt): megaphone_v1 Alternative method to get the current branch.Remote URL: git@github.com:Eon-Labs/ml-feature-set.git The URL of the remote repository named 'origin'.Latest Commit: 57d3a33136d4d721fd8a28971d52c842de2d9a7b - refactor The latest commit hash and message.Status: - M ml_feature_set/features_indicators/indicators_megaphone.py - M ml_feature_set/features_indicators/megaphone_helpers.py - M ml_feature_set/helpers/fetch_binance_ohlcv_parallel.py - M pyproject.toml - ?? CryptoConfigMaster/ The status of the working directory.Remotes: - megaphone ../megaphone (fetch) - megaphone ../megaphone (push) - origin git@github.com:Eon-Labs/ml-feature-set.git (fetch) - origin git@github.com:Eon-Labs/ml-feature-set.git (push) - tl-ml-feature-set ../tl-ml-feature-set (fetch) - tl-ml-feature-set ../tl-ml-feature-set (push) The configured remote repositories and their URLs.
-------------------------------------------------- Environment Variables--------------------------------------------------HOME: /Users/terryliUSER: terryliSHELL: /bin/zshPATH: - /Users/terryli/.pyenv/versions/eon-3.12.5/bin - /Users/terryli/.pyenv/libexec - /Users/terryli/.pyenv/plugins/python-build/bin - /Users/terryli/.pyenv/plugins/pyenv-virtualenv/bin - /Users/terryli/.pyenv/plugins/pyenv-update/bin - /Users/terryli/.pyenv/plugins/pyenv-doctor/bin - /usr/local/hatch/bin - /Users/terryli/.pyenv/plugins/pyenv-virtualenv/shims - /Users/terryli/.pyenv/shims - /Users/terryli/.pyenv/bin - /Users/terryli/.cargo/bin - /Users/terryli/.nvm/versions/node/v21.7.3/bin - /usr/local/opt/tcl-tk/bin - /opt/homebrew/bin - /opt/homebrew/bin - /opt/homebrew/sbin - /usr/bin - /opt/homebrew/bin - /usr/bin - /bin - /usr/sbin - /sbin - /usr/local/bin - /Users/terryli/.local/bin - /Users/terryli/CustomScriptsLANG: en_CA.UTF-8TERM: xterm-256colorThis tutorial should help you create and run a script to log detailed environment information, including explanations for each section of the Git output.