DeviceDetector
DeviceDetector is a precise and fast user agent parser and device detector for Python, backed by a large and frequently updated user agent database. It parses any user agent string to detect the browser, operating system, device used (desktop, tablet, mobile, TV, etc.), brand, and model. Optimized for speed through efficient code and in-memory caching, the library is currently at version 6.2.0 and maintains an active release cadence, primarily updating to incorporate upstream detection rules from the Matomo project.
Common errors
-
ModuleNotFoundError: No module named 'device_detector'
cause The 'device-detector' package has not been installed in the current Python environment.fixInstall the library using pip: `pip install device_detector` -
AttributeError: 'DeviceDetector' object has no attribute 'is_bot'
cause Attempted to access detection methods (e.g., `is_bot()`, `os_name()`) before calling the `parse()` method on the `DeviceDetector` instance. The object needs to process the user agent string first.fixEnsure that `device_detector_instance.parse()` is called before accessing any detection results. Example: `device = DeviceDetector(ua).parse()`. -
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x__ in position __: invalid start byte
cause The user agent string provided contains non-UTF-8 encoded characters that the Python interpreter or the library's internal processing cannot handle by default.fixEnsure the user agent string is correctly encoded. If the origin encoding is known (e.g., 'latin-1', 'windows-1252'), decode it before passing to DeviceDetector, or try to sanitize problematic characters if encoding is unknown.
Warnings
- gotcha The Python port is not an exact 1:1 replica of the original PHP Device Detector library; some Pythonic adaptations were made. Users migrating from or comparing against the PHP version might encounter minor behavioral differences.
- gotcha For optimal performance, especially when processing a large volume of user agents, it is recommended to install `pyyaml` (configured with `libyaml` for `CSafeLoader`) and the `mrab` regex module. Without these, the library will still function but may be slower.
- gotcha The detection logic primarily relies on regex YAML files sourced from the upstream Matomo/PHP Device Detector project. While this ensures up-to-date detection rules, major changes or specific user-agent handling updates in the upstream project will directly reflect in this Python port upon its next release incorporating those changes.
Install
-
pip install device_detector
Imports
- DeviceDetector
from device_detector import DeviceDetector
Quickstart
from device_detector import DeviceDetector
# Example User Agent string
ua = 'Mozilla/5.0 (Linux; Android 4.3; C5502 Build/10.4.1.B.0.101) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.136 Mobile Safari/537.36'
# Initialize and parse the User Agent
device = DeviceDetector(ua).parse()
# Access detected information
print(f"Is bot: {device.is_bot()}")
print(f"OS name: {device.os_name()}")
print(f"OS version: {device.os_version()}")
print(f"Device type: {device.device_type()}")
print(f"Device brand: {device.device_brand()}")
print(f"Device model: {device.device_model()}")
print(f"Browser name: {device.client_name()}")
print(f"Browser version: {device.client_version()}")