正如标题清楚地描述的那样,尽管我已经通过the suggested way成功安装了PyMongo(执行命令python -m pip install pymongo
),当我尝试执行导入ImportError: No module named 'pymongo'
的Python脚本时,得到像PyMongo
这样的错误。
Python
版本:3.7.0
PyMongo
版本:3.7.1
(表示最佳匹配)
附: (1)操作系统是Windows 10。
(2)类似问题的答案不起作用。
编辑:这是命令python -m easy_install pymongo
的输出:
Searching for pymongo
Best match: pymongo 3.7.1
Processing pymongo-3.7.1-py3.7-win-amd64.egg
pymongo 3.7.1 is already the active version in easy-install.pth
Using c:\users\talha\appdata\local\programs\python\python37\lib\site-packages\pymongo-3.7.1-py3.7-win-amd64.egg
Processing dependencies for pymongo
Finished processing dependencies for pymongo
命令python -m pip install pymongo
的输出:
Requirement already satisfied: pymongo in c:\users\talha\appdata\local\programs\python\python37\lib\site-packages\pymongo-3.7.1-py3.7-win-amd64.egg (3.7.1)
我通过使用MonkeyRunner
和PyMongo
模块的Python脚本执行模拟Android设备的命令:
monkeyrunner.bat C:\Users\talha\Documents\PyCharmProjects\MonkeyRunner\test.py
而且,这是完整的错误堆栈跟踪:
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last):
File "C:\Users\talha\Documents\PyCharmProjects\MonkeyRunner\test.py", line 3, in <module>
from pymongo import MongoClient
ImportError: No module named pymongo
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.core.Py.ImportError(Py.java:304)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.core.imp.import_first(imp.java:755)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.core.imp.import_module_level(imp.java:837)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.core.imp.importName(imp.java:917)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.core.ImportFunction.__call__(__builtin__.java:1220)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.core.PyObject.__call__(PyObject.java:357)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.core.__builtin__.__import__(__builtin__.java:1173)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.core.imp.importFromAs(imp.java:1011)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.core.imp.importFrom(imp.java:987)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.pycode._pyx0.f$0(C:\Users\talha\Documents\PyCharmProjects\MonkeyRunner\test.py:39)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.pycode._pyx0.call_function(C:\Users\talha\Documents\PyCharmProjects\MonkeyRunner\test.py)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.core.PyTableCode.call(PyTableCode.java:165)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.core.PyCode.call(PyCode.java:18)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.core.Py.runCode(Py.java:1275)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.core.__builtin__.execfile_flags(__builtin__.java:522)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:225)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at com.android.monkeyrunner.ScriptRunner.run(ScriptRunner.java:116)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at com.android.monkeyrunner.MonkeyRunnerStarter.run(MonkeyRunnerStarter.java:77)
180904 04:34:13.771:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at com.android.monkeyrunner.MonkeyRunnerStarter.main(MonkeyRunnerStarter.java:189)
使用pip
和easy_install
安装的东西最终会出现在不同的地方,具体取决于一些事情。
以下是一些故障排除方法......
添加
import sys ; print(sys.path)
脚本中的某个位置将打印python搜索的(长)路径列表。 pymongo在其中一个目录中吗?
如果您知道它的安装位置,您可以应用以下hack来使其工作:
PYTHONPATH=/path/to/parent/dir python myscript.py
(在Windows中设置环境变量是不同的。可能需要调整。)
使用virtualenv是您可以采取的一次性步骤,至少可以将您的所有问题放在同一个地方。一旦安装并安装完毕,您安装的所有内容都将在您的virtualenv中进行。能够轻松切换环境也很好。
你错过了一个重点:monkeyrunner
是另一个基于jython的python解释器,所以你安装了多少其他python解释器以及使用easy_install
或pip
安装它们的软件包并不重要。
如果你想要一个类似于monkeyrunner
的基于python的解决方案,请检查AndroidViewClient/culebra。