Python pymongo:ImportError:没有名为pymongo的模块

Python pymongo:ImportError:没有名为pymongo的模块,python,mongodb,python-3.x,pymongo,pymongo-3.x,Python,Mongodb,Python 3.x,Pymongo,Pymongo 3.x,我使用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 an

我使用
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$
在谷歌上搜索。在stackoverflow上找到此问题:然后指向stackoverflow的另一页:

然而,即使在这之后,我也面临同样的问题。我无法导入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>
>>> 
>>来自pymongo导入MongoClient
>>>client=MongoClient()
>>>db=client.kmp
>>>db.KMP_DATA.insert_one({“seq”:1,“DATA”:“ABCD”})
>>> 

但是,我仍然无法从python脚本导入它。

常见的问题是,“pip”或“pip3”与“python”或“python3”的环境不同。要确保在运行时使用相同的环境进行安装,请执行以下操作:

python -m pip install pymongo
或:

  • python路径 macOS有自己的python。有时我们会从brew或anaconda安装另一个python。因此,首先您应该知道python使用的是什么
  • 哪个python
    哪条蟒蛇
    哪一点
    哪个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是一对~~~

  • 我的朋友们正在使用虚拟环境

  • 找到实际路径的方法相同。

    打印
    sys.version
    并检查Python版本是否为3.5。如果没有,那么使用python3而不是python,它会工作得很好“>>您使用
    pip3
    安装了
    pymongo
    ,并使用
    python
    运行脚本,我猜这就是
    py2.x
    @styvane,我犯了同样的错误。安装了python3/pip3并使用pyhon运行脚本。
    python -m pip install pymongo
    
    python3 -m pip install pymongo
    
    [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