簡體   English   中英

Python-ImportError:盡管pip表示“要求已滿足”,但沒有名為“ pymongo”的模塊[Windows]

[英]Python - ImportError: No module named 'pymongo' despite pip says “requirement already satisfied” [Windows]

正如標題所清楚描述的那樣,盡管我已經通過建議的方式成功安裝了PyMongo(執行命令python -m pip install pymongo ),卻遇到類似ImportError: No module named 'pymongo'當我嘗試執行導入的Python腳本時, ImportError: No module named 'pymongo' PyMongo

Python版本: 3.7.0

PyMongo版本: 3.7.1 (表示最佳匹配)

ps(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)

pipeasy_install安裝的東西可能會出現在不同的地方,具體取決於一些東西。

以下是一些疑難解答的想法...

添加

import sys ; print(sys.path)

腳本中的某處將打印(長)Python搜索路徑的列表。 pymongo在這些目錄之一中嗎?

如果您知道它的安裝位置,則可以應用以下技巧使其正常運行:

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

(在Windows中設置環境變量是不同的。可能需要進行調整。)

使用virtualenv是一次性的步驟,您至少可以將所有問題都放在同一位置。 安裝並設置好之后,安裝的所有內容都將放在您的virtualenv中。 能夠輕松切換環境也很好。

您遺漏了一個重要的觀點: monkeyrunner是另一個基於jython的python解釋器,因此,無論您安裝了多少其他python解釋器以及使用easy_installpip隨它們安裝了哪些軟件包都沒有關系。

如果您想使用類似於monkeyrunner基於python的解決方案,請檢查AndroidViewClient / culebra

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM