{"id":10114,"library":"pymobiledetect","title":"PyMobileDetect","description":"PyMobileDetect is a Python library that detects mobile and tablet browsers based on the User-Agent string. It is a port of the popular PHP Mobile Detect library. The current version is 1.3.2, with releases being infrequent but stable, primarily aligning with updates to the underlying detection rules.","status":"active","version":"1.3.2","language":"en","source_language":"en","source_url":"https://github.com/srgstr/pymobiledetect","tags":["mobile","user-agent","detection","browser","device","frontend"],"install":[{"cmd":"pip install pymobiledetect","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The PyPI package is 'pymobiledetect', but the module to import is 'mobiledetect'.","wrong":"from pymobiledetect import MobileDetect","symbol":"MobileDetect","correct":"from mobiledetect import MobileDetect"}],"quickstart":{"code":"from mobiledetect import MobileDetect\n\n# Simulate a User-Agent string (e.g., from an HTTP request header)\n# In a web framework, you would get this from request.headers.get('User-Agent', '')\nuser_agent = 'Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Mobile Safari/537.36'\n\ndetector = MobileDetect(user_agent=user_agent)\n\nif detector.is_mobile():\n    print(\"This is a mobile device.\")\nelif detector.is_tablet():\n    print(\"This is a tablet device.\")\nelse:\n    print(\"This is a desktop device.\")\n\n# Check for specific properties\nif detector.is_android():\n    print(\"Operating system: Android\")\nif detector.is_chrome():\n    print(\"Browser: Chrome\")\n","lang":"python","description":"Initialize MobileDetect with a User-Agent string and use its methods to check device type and properties. The User-Agent string is crucial for correct detection."},"warnings":[{"fix":"Always use `from mobiledetect import MobileDetect` after `pip install pymobiledetect`.","message":"The package name on PyPI is `pymobiledetect`, but the Python module you need to import is `mobiledetect`. Importing directly from `pymobiledetect` will result in a `ModuleNotFoundError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure you are using the latest available version of `pymobiledetect`. For critical applications, consider regularly reviewing its detection accuracy against new User-Agent strings and updating the library.","message":"The detection rules are static within the installed library version and do not auto-update. Newer devices, operating systems, or browser versions released after the library's last update might not be correctly identified.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always pass the full and accurate User-Agent string received from the client. Validate or sanitize inputs if there's a risk of malformed data.","message":"Detection accuracy is entirely dependent on the provided User-Agent string. Spoofed, incomplete, or malformed User-Agents will lead to incorrect or failed detection.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Change your import statement from `from pymobiledetect import MobileDetect` to `from mobiledetect import MobileDetect`.","cause":"Attempting to import from 'pymobiledetect' directly, instead of 'mobiledetect'.","error":"ModuleNotFoundError: No module named 'pymobiledetect'"},{"fix":"Provide a User-Agent string when creating an instance, e.g., `detector = MobileDetect(user_agent='...')`. In web applications, this typically comes from `request.headers['User-Agent']`.","cause":"The `MobileDetect` class requires a User-Agent string upon instantiation.","error":"TypeError: MobileDetect() missing 1 required positional argument: 'user_agent'"},{"fix":"Ensure you have the latest version of `pymobiledetect` installed via `pip install --upgrade pymobiledetect`. If the issue persists, the underlying detection rules (inherited from the PHP project) might not yet include data for your specific device.","cause":"The installed version of `pymobiledetect` might not have the latest detection rules for very new devices or browser versions.","error":"My new device is detected as desktop/generic, but it's a mobile!"}]}