gpt4 book ai didi

android - 如何将缺少的模块导入 monkeyrunner 脚本?

转载 作者:行者123 更新时间:2023-11-29 00:41:31 25 4
gpt4 key购买 nike

我在将一个看似存在的模块:simplejson 导入我的 MonkeyRunner 脚本时遇到了一些困难。

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import simplejson

def main():
print "this is a test"

if __name__ == "__main__":
main()

ImportError: 没有名为 simplejson 的模块

据我了解,MonkeyRunner 使用基于 Python 2.5 的 Jython 2.5?我知道 JSON 模块出现在 Python 2.7 中,但我已经在 '/Library/Python/2.5/site-packages/simplejson-2.3.2-py2.5-macosx-10.7-x86_64.egg' 下为 Python 2.5 安装了 simplejson

我的问题是,如何正确地将 simplejson 模块导入到 MonkeyRunner 脚本中?

最佳答案

安装 Python 包不会使其可用于 Jython。

Jython 基于 Python(又名 CPython),前者旨在与后者兼容,但实现方式却大不相同。

可以将 CPython 版本的 simplejson 添加到 Jython 的路径中:

import sys
sys.path.append("/Library/Python/2.5/site-packages/simplejson-2.3.2-py2.5-macosx-10.7-x86_64.egg")
import simplejson

这个“技巧”恰好适用于 simplejson 包(至少对我而言......)。对于其他软件包,它根本不起作用。

我更愿意为 Jython 安装 simplejson。参见 How can I install various Python libraries in Jython?了解详情。

关于android - 如何将缺少的模块导入 monkeyrunner 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9046972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com