【问题标题】:Python - ImportError: No module named 'pymongo' despite pip says "requirement already satisfied" [Windows]Python - ImportError:尽管 pip 说“要求已经满足”,但没有名为“pymongo”的模块 [Windows]
【发布时间】:2019-02-08 23:28:50
【问题描述】:

正如标题清楚地描述的那样,尽管我已经通过the suggested way(执行命令python -m pip install pymongo)成功安装了PyMongo,但当我尝试执行导入PyMongo 的Python 脚本时收到类似ImportError: No module named '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)

我执行的命令通过我的 Python 脚本模拟 Android 设备,该脚本同时使用了 MonkeyRunnerPyMongo 模块

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)

【问题讨论】:

  • 你安装了多个 Python 吗?
  • 不,我不知道,唯一的版本是3.7.0。 @基思
  • 请添加显示错误的代码sn-p,以及回溯。
  • 添加到原帖中。 @基思
  • 你有mongod进程在运行吗?

标签: python pip pymongo monkeyrunner pymongo-3.x


【解决方案1】:

使用pipeasy_install 安装的东西可能会在不同的地方结束,这取决于一些东西。

这里有一些故障排除思路...

添加

import sys ; print(sys.path)

脚本中的某处将打印一个(长)python 搜索的路径列表。 pymongo 是否在这些目录之一中?

如果您知道它的安装位置,则可以应用以下 hack 使其工作:

PYTHONPATH=/path/to/parent/dir python myscript.py

(windows中设置环境变量不同,可能需要调整。)

使用virtualenv 是一次性的步骤,您可以采取至少将所有问题放在同一个地方。一旦你安装并设置好它,你安装的所有东西都将进入你的 virtualenv。能够轻松切换环境也很好。

【讨论】:

  • 是的,该路径包含 PyMongo 文件夹 C:\\Users\\talha\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\pymongo-3.7.1-py3.7-win-amd64.egg,因为我已经手动将显示 PyMongo 安装文件夹的文件夹添加到 PATH 中。
  • 当我使用 Python shell 时,我可以导入 PyMongo。但是当我尝试通过MonkeyRunner(Android 测试工具)执行我的 Python 脚本时,它不起作用,并抛出我在 OP 中提到的错误。
  • PyCharm 可能使用的是完全不同的 python/环境。我不熟悉。如果您使用 pycharm 运行脚本并执行import os; print(os.__file__),这将告诉您 pycharm 正在使用什么 python 环境。
  • 不,我不通过 PyCharm 运行脚本,因为该脚本是由 MonkeyRunner 触发的,它是 Android SDK 下可用的工具。
【解决方案2】:

您遗漏了重要的一点:monkeyrunner 是另一个基于 jython 的 Python 解释器,因此您安装了多少其他 Python 解释器以及使用 easy_installpip 安装了哪些软件包并不重要。

如果您想要类似于monkeyrunner 的基于python 的解决方案,请查看AndroidViewClient/culebra

【讨论】:

  • 刚安装了AndroidViewClient,可惜不支持Python 3
  • 是的,但有 pymongo 用于 2.7.x,因此使用 virtualenvpyenv 您可以创建一个单独的环境来运行您的脚本。
猜你喜欢
  • 2022-11-20
  • 1970-01-01
  • 2021-05-31
  • 2020-08-13
  • 2021-06-30
  • 2013-01-17
  • 1970-01-01
  • 2016-03-20
  • 2018-01-02
相关资源
最近更新 更多