PyMobileDetect

1.3.2 · active · verified Fri Apr 17

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.

Common errors

Warnings

Install

Imports

Quickstart

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")

view raw JSON →