⇦ Back

This page provides a script that can be run to check details about a machine and its Python installation. The distro package is required, so install it from the terminal as follows:

$ python3.13 -m pip install distro

This script can be use to check that:

# Standard library module
from datetime import date
from pip._internal.operations.freeze import freeze
import os
import platform
import pwd
import subprocess
import sys
# Installed package
import distro

today = date.today()
print(f'This script was run on {today}')

# Information about the computer
core_os = platform.system()
print(f'This is a {core_os} machine')
if core_os == 'Linux':
    # Check which version of Python you are running
    major = platform.python_version().split('.')[0]
    minor = platform.python_version().split('.')[1]
    # Check which operating system you have
    if (int(major) == 3) & (int(minor) <= 4):
        # Python 3.4 or lower
        OS = platform.linux_distribution()[0]
        version = platform.linux_distribution()[1]
    else:
        # Check which version of the distro package you have installed
        major = distro.__version__.split('.')[0]
        minor = distro.__version__.split('.')[1]
        if (int(major) <= 1) & (int(minor) <= 4):
            # distro 1.4 or lower
            OS = distro.linux_distribution()[0]
            version = distro.linux_distribution()[1]
        else:
            # distro 1.5.0 or higher
            OS = distro.name()
            version = distro.version()
    print(f'Its OS is {OS}')
    print(f'Its OS version is {version}')
elif core_os == 'Windows':
    OS = platform.system()
    version = platform.release()
    print(f'Its OS version is {version}')
elif core_os == 'Darwin':
    # Check which version of macOS you have
    version = platform.mac_ver()[0]
    major = version.split('.')[0]
    minor = version.split('.')[1]
    if int(major) == 10:
        macOS_vers = {
            '10.11': 'El Capitan',
            '10.12': 'Sierra',
            '10.13': 'High Sierra',
            '10.14': 'Mojave',
            '10.15': 'Catalina',
        }
        OS = macOS_vers[f'{major}.{minor}']
    else:
        macOS_vers = {
            '11': 'Big Sur',
            '12': 'Monterey',
            '13': 'Ventura',
            '14': 'Sonoma',
        }
        OS = macOS_vers[major]
    print(f'Its OS is macOS {OS}')
    print(f'Its OS version is {platform.mac_ver()[0]}')
name = platform.node()
print(f'Its name is {name}')
user = pwd.getpwuid(os.getuid())[0]
print(f'The user is {user}')

# Information about the Git repo
try:
    result = subprocess.run(
        ['git', 'rev-parse', 'HEAD'],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
        # Raise CalledProcessError if command fails
        check=True
    )
    result = result.stdout.strip()
    print('This script is inside a git repo')
    print(f'The git hash is {result}')
except subprocess.CalledProcessError:
    # Not a git repo or other error
    print('This script is not inside a git repo')

# Information about the Python installation
py = platform.python_version()
print(f'The Python version is {py}')
path = sys.executable
print(f'Python is being run from {path}')
print('The installed packages are:')
for requirement in freeze(local_only=True):
    print(requirement)

Formatted output:

# Formatted output
print('\n\n')
if platform.system() == 'Linux':
    print(f'Running with Python {py} on {name}, an {OS} {version} machine, ' +
          f'by user {user}')
    print('\nWorks on:')
    print('┌────────────┬────────────────┬───────────────┬──' + '─' * 25 + '┐')
    print(f'│ {today} │ {OS} {version}   │ Python {py:6} │ {path:16} │')
    print('└────────────┴────────────────┴───────────────┴──' + '─' * 25 + '┘')
elif platform.system() == 'Darwin':
    print(f'Running with Python {py} on {name}, a macOS {OS} machine, ' +
          f'by user {user}')
    print('\nWorks on:')
    if len(sys.executable) > 28:
        path = '...' + sys.executable[-26:]
    else:
        path = sys.executable
    print('┌────────────┬────────────────┬───────────────┬──' + '─' * 29 + '┐')
    print(f'│ {today} │ macOS {OS:8} │ Python {py} │ {path:29} │')
    print('└────────────┴────────────────┴───────────────┴──' + '─' * 29 + '┘')

⇦ Back