在 Windows 中将 MonkeyRunner 导入 Python 脚本失败
为什么在 Windows 中将 MonkeyRunner 导入 Python 脚本失败?
我创建了一个文件 foo.py 并从 MonkeyRunner 文档。在cmd命令行中编译示例代码时出现以下错误:
Traceback (most recent call last): File "foo.py", line 2, in from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice ImportError: No module named com.android.monkeyrunner
Android 工具文件夹中有 Monkeyrunner.jar...,我无法理解如何告诉 Python 从中导入类。
后续
我发现错误是因为我没有最新的Android工具(16)。
- 更新到最新工具 (16) 和platform (15) 首先
- 将 android-sdk\tools 添加到路径中
- 将 foo.py 放入 android-sdk\tools 文件夹中(它似乎在其他地方不起作用)
- 切换 USB 调试
- 在将手机连接到 PC 上
- 输入“monkeyrunner foo.py”
Why importing MonkeyRunner into Python script fails in Windows?
I created a file foo.py and copy-pasted code from the MonkeyRunner docs. The following error appears when compiling the sample code in cmd line:
Traceback (most recent call last): File "foo.py", line 2, in from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice ImportError: No module named com.android.monkeyrunner
The Android tools folder has monkeyrunner.jar... and I fail to understand how to tell Python to import classes from it.
Follow-up
I found out the error came because I didn't have the latest Android tools (16).
- Update to newest tools (16) & platform (15) first
- Add android-sdk\tools into path
- Put foo.py into android-sdk\tools folder (it doesn't seem to work anywhere else)
- Switch usb debugging on
- Attach phone to PC
- Type "monkeyrunner foo.py"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
A) MonkeyRunner 使用 Jython,因此您必须确保已安装它
B) Jython 必须知道 MonkeyRunner 的位置。您可以将该脚本放在 MonkeyRunner 所在的同一位置,也可以将其添加到 Jython 的路径中,这样无论您从何处运行该脚本,Jython 都能够找到它。
A) MonkeyRunner uses Jython, so you must make sure you have that installed
B) Jython must know where MonkeyRunner is. You can either have the script at the same place as where MonkeyRunner is located, or you can add it to Jython's path, so Jython will be able to find it no matter where you run the script from.