pymongo: ImportError: No module named pymongo
pymongo: ImportError: No module named pymongo
我使用 pip3
成功安装了 pymongo
Aleeshas-MacBook-Air:project 2 aleesha$ sudo pip3 install pymongo
The directory '/Users/aleesha/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/aleesha/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pymongo
Downloading pymongo-3.4.0-cp35-cp35m-macosx_10_6_intel.whl (294kB)
100% |████████████████████████████████| 296kB 730kB/s
Installing collected packages: pymongo
Successfully installed pymongo-3.4.0
Aleeshas-MacBook-Air:project 2 aleesha$
然后我在python中写了一个测试脚本写入MongoDB。但是当我执行它时,出现以下错误:
Aleeshas-MacBook-Air:project 2 aleesha$ python pythonScript.py
Traceback (most recent call last):
File "pythonScript.py", line 5, in <module>
from pymongo import MongoClient
ImportError: No module named pymongo
Aleeshas-MacBook-Air:project 2 aleesha$
Python版本:Python 3.5.2
路径是这样的:
>>> import sys
>>> sys.path
['', '/Users/aleesha/Documents', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages']
>>> sys.version
'3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) \n[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]'
>>>
在 google 上搜索过。在 Whosebug 上发现了这个问题:ImportError: No module named 'pymongo' which then points to another page on Whosebug: failure to import pymongo ubuntu
sudo pip uninstall pymongo ==> Succesfully uninstalled
sudo pip uninstall bson ==> Cannot uninstall requirement bson, not installed
sudo apt-get remove python-bson ==> sudo: apt-get: command not found
sudo apt-get remove python-gridfs # not sure if it's necessary ==> sudo: apt-get: command not found
sudo pip install pymongo -U ==> successfully installed pymongo
然而即使在这之后,我仍然面临着同样的问题。我无法导入 pymongo 模块。
编辑: 我正在尝试更多的方法来解决这个问题。使用 IDLE 我可以导入 pymongo 并轻松地将记录插入 MongoDB.
>>> from pymongo import MongoClient
>>> client = MongoClient()
>>> db = client.kmp
>>> db.KMP_DATA.insert_one({"seq" : 1,"data" : "ABCD"})
<pymongo.results.InsertOneResult object at 0x1058c8828>
>>>
但是我仍然无法从 python 脚本导入它。
"pip" 或 "pip3" 与 "python" 或 "python3" 的环境不同是一个常见问题。确保您使用与 运行:
相同的环境进行安装
python -m pip install pymongo
或者:
python3 -m pip install pymongo
- python路径
macOS 有自己的 python。有时我们会从 brew 或 anaconda 安装另一个 python。所以首先你应该 python 正在使用。
which python
which python3
which pip
which pip3
命令 python
、python3
、pip
和 pip3
,可以是路径、别名或软链接。
如果是一条路,你就找到了;
如果它是别名或软链接,你应该使用 ls -l xxx
找到 python
的真实路径
示例:
[morningtzh@mbajiushigelaji:~]
% which python
python: aliased to /Users/morningtzh/anaconda/bin/python3
[morningtzh@mbajiushigelaji:~]
% ls -l /Users/morningtzh/anaconda/bin/python3
lrwxr-xr-x 1 morningtzh staff 9 1 8 14:41 /Users/morningtzh/anaconda/bin/python3 -> python3.6
[morningtzh@mbajiushigelaji:~]
% ls -l /Users/morningtzh/anaconda/bin/python3.6
-rwxrwxr-x 1 morningtzh staff 3937816 1 8 14:41 /Users/morningtzh/anaconda/bin/python3.6
[morningtzh@mbajiushigelaji:~]
% which pip3
/Users/morningtzh/anaconda/bin/pip3
[morningtzh@mbajiushigelaji:~]
% ls -l /Users/morningtzh/anaconda/bin/pip3
-rwxr-xr-x 1 morningtzh staff 237 10 16 16:16 /Users/morningtzh/anaconda/bin/pip3
示例显示我的python和pip是一对~~~
- 我的你在使用虚拟环境
同理找到真实路径
我使用 pip3
Aleeshas-MacBook-Air:project 2 aleesha$ sudo pip3 install pymongo
The directory '/Users/aleesha/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/aleesha/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pymongo
Downloading pymongo-3.4.0-cp35-cp35m-macosx_10_6_intel.whl (294kB)
100% |████████████████████████████████| 296kB 730kB/s
Installing collected packages: pymongo
Successfully installed pymongo-3.4.0
Aleeshas-MacBook-Air:project 2 aleesha$
然后我在python中写了一个测试脚本写入MongoDB。但是当我执行它时,出现以下错误:
Aleeshas-MacBook-Air:project 2 aleesha$ python pythonScript.py
Traceback (most recent call last):
File "pythonScript.py", line 5, in <module>
from pymongo import MongoClient
ImportError: No module named pymongo
Aleeshas-MacBook-Air:project 2 aleesha$
Python版本:Python 3.5.2
路径是这样的:
>>> import sys
>>> sys.path
['', '/Users/aleesha/Documents', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages']
>>> sys.version
'3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) \n[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]'
>>>
在 google 上搜索过。在 Whosebug 上发现了这个问题:ImportError: No module named 'pymongo' which then points to another page on Whosebug: failure to import pymongo ubuntu
sudo pip uninstall pymongo ==> Succesfully uninstalled
sudo pip uninstall bson ==> Cannot uninstall requirement bson, not installed
sudo apt-get remove python-bson ==> sudo: apt-get: command not found
sudo apt-get remove python-gridfs # not sure if it's necessary ==> sudo: apt-get: command not found
sudo pip install pymongo -U ==> successfully installed pymongo
然而即使在这之后,我仍然面临着同样的问题。我无法导入 pymongo 模块。
编辑: 我正在尝试更多的方法来解决这个问题。使用 IDLE 我可以导入 pymongo 并轻松地将记录插入 MongoDB.
>>> from pymongo import MongoClient
>>> client = MongoClient()
>>> db = client.kmp
>>> db.KMP_DATA.insert_one({"seq" : 1,"data" : "ABCD"})
<pymongo.results.InsertOneResult object at 0x1058c8828>
>>>
但是我仍然无法从 python 脚本导入它。
"pip" 或 "pip3" 与 "python" 或 "python3" 的环境不同是一个常见问题。确保您使用与 运行:
相同的环境进行安装python -m pip install pymongo
或者:
python3 -m pip install pymongo
- python路径 macOS 有自己的 python。有时我们会从 brew 或 anaconda 安装另一个 python。所以首先你应该 python 正在使用。
which python
which python3
which pip
which pip3
命令 python
、python3
、pip
和 pip3
,可以是路径、别名或软链接。
如果是一条路,你就找到了;
如果它是别名或软链接,你应该使用 ls -l xxx
找到 python
示例:
[morningtzh@mbajiushigelaji:~]
% which python
python: aliased to /Users/morningtzh/anaconda/bin/python3
[morningtzh@mbajiushigelaji:~]
% ls -l /Users/morningtzh/anaconda/bin/python3
lrwxr-xr-x 1 morningtzh staff 9 1 8 14:41 /Users/morningtzh/anaconda/bin/python3 -> python3.6
[morningtzh@mbajiushigelaji:~]
% ls -l /Users/morningtzh/anaconda/bin/python3.6
-rwxrwxr-x 1 morningtzh staff 3937816 1 8 14:41 /Users/morningtzh/anaconda/bin/python3.6
[morningtzh@mbajiushigelaji:~]
% which pip3
/Users/morningtzh/anaconda/bin/pip3
[morningtzh@mbajiushigelaji:~]
% ls -l /Users/morningtzh/anaconda/bin/pip3
-rwxr-xr-x 1 morningtzh staff 237 10 16 16:16 /Users/morningtzh/anaconda/bin/pip3
示例显示我的python和pip是一对~~~
- 我的你在使用虚拟环境
同理找到真实路径