fake-useragent is a Python library that generates up-to-date and realistic browser user-agent strings for web scraping and testing purposes. It dynamically fetches a database of real-world user agents to provide diverse and current options. The current version is 2.2.0, with minor releases occurring every few months to update the user-agent database or fix small issues.
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
muslpy 3.10–3.930 runs
installs and imports cleanly · install 0.0s · import 0.146s · 20.4MB
glibcpy 3.10–3.930 runs
installs and imports cleanly · install 1.6s · import 0.130s · 21MB
18MB installed
● package 18MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
from fake_useragent import UserAgent
Exception for handling errors like network issues during data fetching.
from fake_useragent import FakeUserAgentError
This quickstart demonstrates how to initialize `UserAgent` and retrieve various types of user-agent strings. It includes basic error handling for potential network issues during the initial data download or cache refresh.
from fake_useragent import UserAgent, FakeUserAgentError
try:
ua = UserAgent()
print(f"Random User-Agent: {ua.random}")
print(f"Chrome User-Agent: {ua.chrome}")
print(f"Firefox User-Agent: {ua.firefox}")
print(f"User-Agent for macOS: {ua.get_ua('chrome', os='macos')}")
print(f"User-Agent for desktop: {ua.get_ua(platform='desktop')}")
except FakeUserAgentError as e:
print(f"Error fetching user agents: {e}")
print("This might be due to network issues or the first-time data download failing.")
Upgrade
Version history
Breaking-change detection hasn't run for this library yet.
Audit
Security & dependencies
CVE tracking and dependency tree are planned for a later release.