py-machineid
py-machineid is a Python library designed to retrieve the unique machine ID (UUID/GUID) of any host without requiring administrative privileges. It extracts the operating system's native machine identifier, which is generally stable for a given OS installation. The library also offers a cryptographically hashed version of the ID, useful for application-specific identification. The current version is 1.0.0, and it maintains an active development status, with updates released as needed for bug fixes and feature enhancements.
Common errors
-
ModuleNotFoundError: No module named 'machineid'
cause The `py-machineid` package has not been installed in your current Python environment.fixRun `pip install py-machineid` in your terminal to install the library. -
TypeError: 'str' object is not callable
cause You are attempting to use `machineid.id` or `machineid.hashed_id` as a variable or property rather than calling it as a function. Functions require parentheses `()` to be executed.fixCall the functions with parentheses: `machineid.id()` or `machineid.hashed_id('your-app-id')`. -
ValueError: Invalid application ID, must be a string.
cause You passed a non-string value (e.g., `None` or an integer) to the `hashed_id()` function as the application ID. The `app_id` parameter must be a string.fixEnsure that the `app_id` argument passed to `machineid.hashed_id()` is always a string, for example: `machineid.hashed_id('my_application')`.
Warnings
- breaking The `hashed_id()` function's order of operations for HMAC computation was fixed in v1.0.0 to match the Go `machineid` package. This is a breaking change for all previously generated hashed IDs.
- gotcha On Windows, `id()` and `hashed_id()` query the registry by default. This behavior can be disabled using the `winregistry=False` keyword argument. Disabling it might prevent manual modification of machine fingerprints but could alter the ID generated.
- gotcha Machine IDs, while generally stable per OS installation, can be changed by a root/admin user (e.g., by modifying `/var/lib/dbus/machine-id` on Linux or using `sysprep` on Windows). Cloned or imaged systems might also retain the original ID if not properly prepared.
- gotcha A `SyntaxWarning: invalid escape sequence '\S'` may be emitted when importing `machineid` with Python 3.12 due to a regex pattern.
- gotcha The machine ID uniquely identifies the host and should be treated as confidential information. Directly exposing it in untrusted environments is a security risk.
Install
-
pip install py-machineid
Imports
- id
import machineid machineid.id()
- hashed_id
import machineid machineid.hashed_id('your-application-id')
Quickstart
import machineid
# Get the raw, native machine ID (GUID/UUID)
try:
device_id = machineid.id()
print(f"Raw Machine ID: {device_id}")
except Exception as e:
print(f"Error getting raw machine ID: {e}")
# Get an anonymized (hashed) version of the ID for a specific application
app_id = "my-unique-application"
try:
hashed_device_id = machineid.hashed_id(app_id)
print(f"Hashed Machine ID (for '{app_id}'): {hashed_device_id}")
except Exception as e:
print(f"Error getting hashed machine ID: {e}")
# Disable Windows registry query (if applicable) for id()
try:
win_id_no_reg = machineid.id(winregistry=False)
print(f"Raw Machine ID (no winregistry): {win_id_no_reg}")
except Exception as e:
print(f"Error getting raw machine ID (no winregistry): {e}")