Mozdevice (Mozilla Device Management)
Mozdevice is a Python library developed by Mozilla for managing and interacting with mobile devices (Android, Firefox OS/B2G) via ADB, as well as desktop browsers. It provides functionalities for device introspection, application management, and remote command execution, primarily used in automated testing and development workflows. The current version is 4.2.0, with an active release cadence driven by Mozilla's mobile testing needs. It requires Python 3.6 or newer.
Common errors
-
mozdevice.errors.ADBError: ADB executable not found. Make sure it's in your PATH or specify ADB_EXEC_PATH.
cause The ADB executable is not found in the system's PATH or ADB_EXEC_PATH environment variable, or it's not executable.fixInstall Android SDK Platform-Tools and ensure the directory containing `adb` is in your system's PATH. Alternatively, set the `ADB_EXEC_PATH` environment variable to the full path of the `adb` executable before running your Python script. -
mozdevice.errors.DeviceConnectionError: No devices attached or available.
cause Mozdevice could not find any connected or specified device via ADB.fixEnsure your device is physically connected, USB debugging is enabled, and the device is authorized for ADB debugging. Run `adb devices` in your terminal to confirm the device is listed. If connecting remotely, verify IP/port and network accessibility.
Warnings
- gotcha Mozdevice relies on ADB (Android Debug Bridge) being installed and accessible in your system's PATH. Without a correctly set up ADB environment, device connection will fail.
- breaking The minimum Python version required is 3.6. While PyPI metadata might sometimes show `None` for `requires_python`, the project's `setup.cfg` explicitly states `>=3.6`.
- gotcha Connecting to specific devices when multiple are present or connecting to remote devices requires specifying connection parameters (e.g., `serial`, `addr`, `port`) during `DeviceManager` initialization.
Install
-
pip install mozdevice
Imports
- DeviceManager
from mozdevice import DeviceManager
from mozdevice.devicemanager import DeviceManager
Quickstart
import os
from mozdevice.devicemanager import DeviceManager
try:
# Connect to a locally available ADB device
# If multiple devices are connected, you might need to specify 'serial'
dm = DeviceManager()
print(f"Connected to device: {dm.name}")
print(f"Device type: {dm.device_type}")
print(f"Device OS: {dm.os}")
# Example: Get a list of installed packages (Android)
if dm.os == 'android':
packages = dm.list_packages()
print(f"Number of installed packages: {len(packages)}")
# print(f"Sample packages: {packages[:5]}")
except Exception as e:
print(f"Error connecting or interacting with device: {e}")
print("Please ensure ADB is installed and a device is connected and authorized.")