Registry / http-networking / user-agents

user-agents

JSON →
library2.2.0pypypi✓ verified 29d ago

The `user-agents` library is a simple Python library for identifying devices (phones, tablets, PCs) and their capabilities by parsing browser user agent strings. It wraps the `ua-parser` library and provides a more convenient object-oriented interface. The current version is 2.2.0, and releases are made periodically to incorporate updates from `ua-parser` and add minor features.

pip install user-agents
INSTALL
IMPORT
SIG · USER-AGENTS
U
user-agents
http-networkingpythonv2.2.0
Install
1.7s avg
Import
796ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v2.2.0 · 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.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.822s · 18.8MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.770s · 19MB
17MB installed
● package 17MB
Code
Verified usage

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

parse
✓ from user_agents import parse

This quickstart demonstrates how to parse different user agent strings using `user_agents.parse` and access various properties like device type, operating system, and browser information. It also shows how the library handles empty strings gracefully, returning an object with 'Other' or empty values.

from user_agents import parse # Example user agent strings user_agent_string_mobile = 'Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Mobile Safari/537.36' user_agent_string_desktop = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' user_agent_string_empty = '' # Parse a mobile user agent user_agent_mobile = parse(user_agent_string_mobile) print(f"Mobile UA: {user_agent_mobile.is_mobile} (Mobile)") print(f"Device: {user_agent_mobile.device.family}, OS: {user_agent_mobile.os.family} {user_agent_mobile.os.version_string}") print(f"Browser: {user_agent_mobile.browser.family} {user_agent_mobile.browser.version_string}\n") # Parse a desktop user agent user_agent_desktop = parse(user_agent_string_desktop) print(f"Desktop UA: {user_agent_desktop.is_pc} (PC)") print(f"Device: {user_agent_desktop.device.family}, OS: {user_agent_desktop.os.family} {user_agent_desktop.os.version_string}") print(f"Browser: {user_agent_desktop.browser.family} {user_agent_desktop.browser.version_string}\n") # Parse an empty user agent user_agent_empty = parse(user_agent_string_empty) print(f"Empty UA is_pc: {user_agent_empty.is_pc}") print(f"Empty UA device family: {user_agent_empty.device.family}")
Debug
Known issues
breakingVersion 2.2.0 and newer explicitly require `ua-parser >= 0.10.0`. If you have an older version of `ua-parser` pinned in your environment, upgrading `user-agents` might lead to dependency conflicts or installation failures.
fix
Ensure your `ua-parser` dependency is updated to at least `0.10.0` or allow pip to upgrade it automatically (`pip install --upgrade user-agents`). Consider using a virtual environment to manage dependencies.
affects: >=2.2.0
gotchaParsing an empty or `None` user agent string does not raise an error. Instead, `parse('')` returns a `UserAgent` object where properties like `is_pc`, `is_mobile`, `browser.family`, `os.family`, etc., will default to `False`, `None`, or 'Other'/'Unknown' respectively. This might not be the expected behavior for users anticipating an error or `None`.
fix
Always check the returned `UserAgent` object's properties (e.g., `user_agent.is_unknown` or `user_agent.device.family == 'Other'`) if you need to specifically handle empty or unidentifiable user agents. You may want to implement explicit checks for empty strings before calling `parse()` if an error is preferred.
affects: All
gotchaThe underlying user agent definitions are provided by the `ua-parser` library. For the most accurate and up-to-date parsing, it's crucial that `ua-parser` (and thus `user-agents`) is kept up-to-date. Outdated versions may incorrectly identify newer devices or browsers.
fix
Regularly upgrade the `user-agents` package (`pip install --upgrade user-agents`) to ensure you have the latest `ua-parser` definitions available. The `user-agents` library's `install_requires` ensures a compatible `ua-parser` version is installed.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'user_agents'
The `user-agents` library is not installed in your current Python environment, or the package name was mistyped during installation.
fix
Install the library using pip: `pip install user-agents`
AttributeError: 'bool' object has no attribute '__call__'
You are attempting to call a boolean attribute (like `is_mobile`, `is_pc`, `is_tablet`, `is_bot`) as if it were a method, but it is a direct boolean value.
fix
Access these attributes directly without parentheses: `if user_agent.is_mobile: ...`
NameError: name 'parse' is not defined
You imported the `user_agents` module as `import user_agents` but then tried to use `parse()` directly without specifying the module prefix.
fix
Either import `parse` directly: `from user_agents import parse` and then use `parse(ua_string)`, or use `user_agents.parse(ua_string)` after `import user_agents`.
AttributeError: module 'user_agents' has no attribute 'browser'
You are trying to access attributes like `browser`, `os`, or `device` directly on the `user_agents` module instead of on the `UserAgent` object returned by the `parse` function.
fix
First, parse the user agent string to get a `UserAgent` object, then access its attributes: `user_agent = parse(ua_string); print(user_agent.browser.family)`
Upgrade
Version history
2.2.0latest on PyPI · released Aug 23, 2020
Audit
Dependencies
ua-parserrequiredCore dependency for parsing user-agent strings and providing definition files.
Agent activity
20 hits · last 30 days
node
16
OpenAI (training)
1
Resources
user-agents — pip install user-agents · libregistry