{"id":8935,"library":"device-detector","title":"DeviceDetector","description":"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.","status":"active","version":"6.2.0","language":"en","source_language":"en","source_url":"https://github.com/thinkwelltwd/device_detector.git","tags":["user agent","device detection","browser detection","os detection","client hints","mobile detection"],"install":[{"cmd":"pip install device_detector","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Used for CSafeLoader, which offers performance benefits if configured with libyaml.","package":"pyyaml","optional":true},{"reason":"The 'mrab' regex module is preferred for faster performance if installed.","package":"mrab","optional":true}],"imports":[{"symbol":"DeviceDetector","correct":"from device_detector import DeviceDetector"}],"quickstart":{"code":"from device_detector import DeviceDetector\n\n# Example User Agent string\nua = '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'\n\n# Initialize and parse the User Agent\ndevice = DeviceDetector(ua).parse()\n\n# Access detected information\nprint(f\"Is bot: {device.is_bot()}\")\nprint(f\"OS name: {device.os_name()}\")\nprint(f\"OS version: {device.os_version()}\")\nprint(f\"Device type: {device.device_type()}\")\nprint(f\"Device brand: {device.device_brand()}\")\nprint(f\"Device model: {device.device_model()}\")\nprint(f\"Browser name: {device.client_name()}\")\nprint(f\"Browser version: {device.client_version()}\")","lang":"python","description":"Initializes `DeviceDetector` with a user agent string, parses it, and demonstrates how to access various detected properties like OS, device type, brand, and browser."},"warnings":[{"fix":"Consult the Python library's documentation and examples for specific method names and return types.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Install `pyyaml` (with C extension support) and `mrab`: `pip install pyyaml mrab`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Keep the library updated to benefit from the latest upstream detection rules. Monitor upstream project changes for potential impacts.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install the library using pip: `pip install device_detector`","cause":"The 'device-detector' package has not been installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'device_detector'"},{"fix":"Ensure that `device_detector_instance.parse()` is called before accessing any detection results. Example: `device = DeviceDetector(ua).parse()`.","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.","error":"AttributeError: 'DeviceDetector' object has no attribute 'is_bot'"},{"fix":"Ensure 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.","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.","error":"UnicodeDecodeError: 'utf-8' codec can't decode byte 0x__ in position __: invalid start byte"}]}