Registry / http-networking / pymobiledetect

pymobiledetect

JSON →
library1.3.2pypypi✓ verified 87d ago

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.

pip install pymobiledetect
INSTALL
IMPORT
SIG · PYMOBILEDETECT
P
pymobiledetect
http-networkingpythonv1.3.2
Install
1.7s avg
Import
222ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.223s · 18.4MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.7s · import 0.220s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MobileDetect
from mobiledetect import MobileDetect
from pymobiledetect import MobileDetect
The PyPI package is 'pymobiledetect', but the module to import is 'mobiledetect'.

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.

from mobiledetect import MobileDetect # Simulate a User-Agent string (e.g., from an HTTP request header) # In a web framework, you would get this from request.headers.get('User-Agent', '') user_agent = 'Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Mobile Safari/537.36' detector = MobileDetect(user_agent=user_agent) if detector.is_mobile(): print("This is a mobile device.") elif detector.is_tablet(): print("This is a tablet device.") else: print("This is a desktop device.") # Check for specific properties if detector.is_android(): print("Operating system: Android") if detector.is_chrome(): print("Browser: Chrome")
Debug
Known issues
gotchaThe 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`.
fix
Always use `from mobiledetect import MobileDetect` after `pip install pymobiledetect`.
affects: All versions
gotchaThe 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.
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.
affects: All versions
gotchaDetection accuracy is entirely dependent on the provided User-Agent string. Spoofed, incomplete, or malformed User-Agents will lead to incorrect or failed detection.
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.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pymobiledetect'
Attempting to import from 'pymobiledetect' directly, instead of 'mobiledetect'.
fix
Change your import statement from `from pymobiledetect import MobileDetect` to `from mobiledetect import MobileDetect`.
TypeError: MobileDetect() missing 1 required positional argument: 'user_agent'
The `MobileDetect` class requires a User-Agent string upon instantiation.
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']`.
My new device is detected as desktop/generic, but it's a mobile!
The installed version of `pymobiledetect` might not have the latest detection rules for very new devices or browser versions.
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.
Upgrade
Version history
1.3.2latest on PyPI · released May 10, 2018
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
pymobiledetect — pip install pymobiledetect · libregistry