简体   繁体   English

Python(或MonkeyRunner)如何定位导入的模块?

[英]How does Python (or MonkeyRunner) locate imported modules?

Update: Following @dtmilano's suggestion, I added 更新:按照@dtmilano的建议,我添加了

import sys
print(sys.path)

to the beginning of my MonkeyRunner script. 到我的MonkeyRunner脚本的开头。 This results in 这导致

['e:/path/android-sdk/tools/lib/monkeyrunner.jar:e:\\path\\bbct\\android\\functional-tests', 'E:\\path\\android-sdk\\tools\\lib\\Lib', '/E:/path/android-sdk/tools/lib/jython-standalone-2.5.3.jar/Lib', '__classpath__', '__pyclasspath__/']

At first glance I thought this included the current working directory. 乍一看,我认为这包括当前的工作目录。 However, a closer inspection showed that the output is a list of strings where the first string is 但是,仔细检查发现,输出是一个字符串列表,其中第一个字符串是

'e:/path/android-sdk/tools/lib/monkeyrunner.jar:e:\\path\\bbct\\android\\functional-tests'

For some reason this contains two paths concatenated together. 由于某些原因,它包含两个串联在一起的路径。 Is this a bug in MonekyRunner and/or Jython? 这是MonekyRunner和/或Jython中的错误吗?

Original Question: I have two .py files in the same directory: screenshots.py and util.py . 原始问题:我在同一目录中有两个.py文件: screenshots.pyutil.py I need to run screenshots.py with the monkeyrunner interpreter from the Android build tools. 我需要使用Android构建工具中的monkeyrunner解释器运行screenshots.py。 When I run monkeyrunner screenshots.py , I get error No module named util . 当我运行monkeyrunner screenshots.py ,出现错误No module named util How do I configure my python and/or monkeyrunner to find the code in util.py ? 如何配置python和/或util.pyutil.py找到代码?

Edit: I am using Git Bash on Win7. 编辑:我在Win7上使用Git Bash。 (Oops, I probably should have mentioned this earlier.) (糟糕,我可能早先应该提到这一点。)

For reference, this is the complete error message: 供参考,这是完整的错误消息:

130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]脚本由于异常而终止
130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last): 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]追踪(最近一次通话):
File "c:\\Users\\Dell\\Documents\\dev\\src\\java\\bbct\\android\\functional-tests\\screenshots.py", line 19, in 文件“ c:\\ Users \\ Dell \\ Documents \\ dev \\ src \\ java \\ bbct \\ android \\ functional-tests \\ screenshots.py”,第19行,在
import util 导入工具
ImportError: No module named util ImportError:没有名为util的模块

130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.Py.ImportError(Py.java:264) 在org.python.core.Py.ImportError(Py.java:264)
130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.imp.import_first(imp.java:657) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 在org.python.core.imp.import_first(imp.java:657)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.imp.import_name(imp.java:741) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 在org.python.core.imp.import_name(imp.java:741)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.imp.importName(imp.java:791) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 在org.python.core.imp.importName(imp.java:791)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.ImportFunction. 在org.python.core.ImportFunction中。 call (_ builtin _.java:1236) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 呼叫 (_ 内置 _.java:1236)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyObject. 在org.python.core.PyObject。 call (PyObject.java:367) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 致电 (PyObject.java:367)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core. 在org.python.core。 builtin ._ import _( builtin .java:1207) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 内置 ._ 导入 _( 内置 .java:1207)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core. 在org.python.core。 builtin ._ import _( builtin .java:1190) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 内置 ._ 导入 _( 内置 .java:1190)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.imp.importOne(imp.java:802) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 在org.python.core.imp.importOne(imp.java:802)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.pycode._pyx0.f$0(c:\\Users\\Dell\\Documents\\dev\\src\\java\\bbct\\android\\functional-tests\\screenshots.py:51) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 在org.python.pycode._pyx0.f $ 0(c:\\ Users \\ Dell \\ Documents \\ dev \\ src \\ java \\ bbct \\ android \\ functional-tests \\ screenshots.py:51)130807 12:01:59.978:S [主要] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.pycode._pyx0.call_function(c:\\Users\\Dell\\Documents\\dev\\src\\java\\bbct\\android\\functional-tests\\screenshots.py) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 在org.python.pycode._pyx0.call_function(c:\\ Users \\ Dell \\ Documents \\ dev \\ src \\ java \\ bbct \\ android \\ functional-tests \\ screenshots.py)130807 12:01:59.978:S [main] [ com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyTableCode.call(PyTableCode.java:165) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 在org.python.core.PyTableCode.call(PyTableCode.java:165)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyCode.call(PyCode.java:18) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 在org.python.core.PyCode.call(PyCode.java:18)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.Py.runCode(Py.java:1197) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 在org.python.core.Py.runCode(Py.java:1197)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core. 在org.python.core。 builtin .execfile_flags( builtin .java:538) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 内置 .execfile_flags( 内置 .java:538)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:156) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 在org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:156)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at com.android.monkeyrunner.ScriptRunner.run(ScriptRunner.java:116) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 在com.android.monkeyrunner.ScriptRunner.run(ScriptRunner.java:116)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at com.android.monkeyrunner.MonkeyRunnerStarter.run(MonkeyRunnerStarter.java:77) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] 在com.android.monkeyrunner.MonkeyRunnerStarter.run(MonkeyRunnerStarter.java:77)130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at com.android.monkeyrunner.MonkeyRunnerStarter.main(MonkeyRunnerStarter.java:189) 在com.android.monkeyrunner.MonkeyRunnerStarter.main(MonkeyRunnerStarter.java:189)

You need to add the module to the search path (you are cwd is not in the same dir?) 您需要将模块添加到搜索路径(您cwd是否不在同一目录中?)

http://docs.python.org/2/tutorial/modules.html http://docs.python.org/2/tutorial/modules.html

import sys sys.path.append('/path/to/your/module') 导入sys sys.path.append('/ path / to / your / module')

The directory where the script is located is automatically added to sys.path by monkeyrunner (and this works pretty well for other OSs, unfortunately it seems you are using one where it doesn't). 该脚本所在的目录由monkeyrunner自动添加到sys.path (这在其他OS上效果很好,不幸的是,您似乎正在使用不使用它的目录)。 Then other modules present in the same directory of the script can be imported without problems. 然后,可以毫无问题地导入脚本同一目录中存在的其他模块。

That is, screenshots.py should find utils.py because the directory functional-tests is in sys.path . 也就是说, screenshots.py应该找到utils.py因为目录functional-testssys.path

You can verify its content by doing 您可以通过执行以下操作来验证其内容

import sys
print sys.path
import util

in screenshots.py . screenshots.py My guess is that all those c:\\ are messing with the path. 我的猜测是所有这些c:\\都弄乱了路径。

For your reference, the android SDK does appear to have a bug in it where the first item on sys.path is the monkeyRunner lib and the working directory mashed together. 供您参考,android SDK似乎确实存在一个错误,其中sys.path的第一项是monkeyRunner lib和工作目录混在一起。 I added the following to fix. 我添加了以下内容进行修复。

import sys
sys.path.append(sys.path[0].split(':',1)[1])
import util

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM