2025-09-13 08:34PM
我运行 python -m uiautomator2 init 命令报错了,内容如下:
$ python -m uiautomator2 init
/usr/lib/python3/dist-packages/requests/__init__.py:87: RequestsDependencyWarning: urllib3 (2.4.0) or chardet (4.0.0) doesn't match a supported version!
warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
[D 2025-09-13 17:49:58,490 core:254 pid:15404] file u2.jar md5: 3564c56e4ef077f92c0fd32bcb42aea1
[D 2025-09-13 17:49:58,572 core:243 pid:15404] file u2.jar already pushed
[D 2025-09-13 17:49:58,572 core:111 pid:15404] http request GET /ping None
[D 2025-09-13 17:49:58,611 core:254 pid:15404] file u2.jar md5: 3564c56e4ef077f92c0fd32bcb42aea1
[D 2025-09-13 17:49:58,709 core:243 pid:15404] file u2.jar already pushed
[D 2025-09-13 17:49:58,709 _input:71 pid:15404] installing AdbKeyboard ime
Traceback (most recent call last):
File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.10/dist-packages/uiautomator2/__main__.py", line 277, in <module>
main()
File "/usr/local/lib/python3.10/dist-packages/uiautomator2/__main__.py", line 269, in main
actions[args.subparser](args)
File "/usr/local/lib/python3.10/dist-packages/uiautomator2/__main__.py", line 35, in cmd_init
d._setup_ime()
File "/usr/local/lib/python3.10/dist-packages/uiautomator2/_input.py", line 74, in _setup_ime
self.adb_device.install(str(ime_apk_path), nolaunch=True, uninstall=True)
File "/usr/local/lib/python3.10/dist-packages/adbutils/install.py", line 64, in install
self._install(path_or_url, nolaunch, uninstall, silent, callback, flags, has_apkutils)
File "/usr/local/lib/python3.10/dist-packages/adbutils/install.py", line 111, in _install
activities = apk.get_main_activities()
AttributeError: 'APK' object has no attribute 'get_main_activities'
解决方法:
1. 降级冲突的库到兼容版本
# 降级 urllib3 到与 selenium 兼容的 2.4.x 版本
pip install urllib3==2.4.0
# 降级 adbutils 到与 uiautomator2 兼容的 2.9.x 版本 (例如 2.9.4)
pip install adbutils==2.9.4
2. 使用虚拟环境
你遇到的 requests
无法卸载的问题(Not uninstalling requests at /usr/lib/python3/dist-packages, outside environment /usr
)以及 pip 的警告(WARNING: Running pip as the 'root' user...
),都强烈建议你使用虚拟环境来管理项目依赖。
虚拟环境可以为你每个项目创建独立的Python运行环境,避免项目间的依赖冲突,也无需管理员权限。
创建并激活虚拟环境的步骤:
# 安装虚拟环境工具(如果尚未安装)
$ pip install virtualenv
$ apt install python3.10-venv
# 创建名为 'venv' 的虚拟环境(你可以取任何名字)
$ python -m venv venv
# 激活虚拟环境 (Linux/macOS)
$ source venv/bin/activate
# 激活后,你的命令行提示符前通常会显示 (venv)
# 然后在激活的虚拟环境中重新安装你项目需要的包,例如:
# pip install uiautomator2
# pip install selenium
# ...以及其他依赖
最后在运行 $ python -m uiautomator2 init 命令
$ python -m uiautomator2 init
[D 2025-09-13 18:01:05,506 core:254 pid:15680] file u2.jar md5: 3564c56e4ef077f92c0fd32bcb42aea1
[D 2025-09-13 18:01:05,603 core:243 pid:15680] file u2.jar already pushed
[D 2025-09-13 18:01:05,603 core:111 pid:15680] http request GET /ping None
[D 2025-09-13 18:01:05,645 core:254 pid:15680] file u2.jar md5: 3564c56e4ef077f92c0fd32bcb42aea1
[D 2025-09-13 18:01:05,725 core:243 pid:15680] file u2.jar already pushed
[D 2025-09-13 18:01:05,726 _input:71 pid:15680] installing AdbKeyboard ime
apkutils is not installed, install it with 'pip install adbutils[apk]'
push apk to device: /data/local/tmp/unknown.apk
100.0% 23.9 MB/s [1.8 MB/1.8 MB]
100.0% 0.0 MB/s [1.8 MB/1.8 MB]
install to android system ...
successfully installed, time used 10.4 seconds
[D 2025-09-13 18:01:16,234 base:110 pid:15680] shell: ime list -s -a
[D 2025-09-13 18:01:16,612 base:110 pid:15680] shell: ime list -s -a
[D 2025-09-13 18:01:16,994 base:110 pid:15680] shell: ime list -s -a
然后手机就会提示要安装 ATX 了,点击继续安装就可以了
登录
请登录后再发表评论。
评论列表:
目前还没有人发表评论