Python 导入错误:没有名为“pymongo”的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22885593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: No module named 'pymongo'
提问by user3151858
I have a problem running pymongo on Win 7 (64) with Python 3.4, mongodb 4.2.10. The error output is as follows:
我在使用 Python 3.4、mongodb 4.2.10 在 Win 7 (64) 上运行 pymongo 时遇到问题。错误输出如下:
import pymongo
ImportError: No module named 'pymongo'
The code is pretty simple:
代码非常简单:
import pymongo
from pymongo import MongoClient
client=MongoClient()
db=client.test_db
dict={'A':[1,2,3,4,5,6]}
db.test_collection.insert(dict)
to_print=db.test_collection.find()
print(to_print)
I tried already re-installing Python and MongoDB - did not help. It works when I do it manually in cmd, i.e. mongod.exe and mongo.exe work fine. It appears there is problem with pymongo, but I don't know how to fix it.
我已经尝试重新安装 Python 和 MongoDB - 没有帮助。当我在 cmd 中手动执行它时它可以工作,即 mongod.exe 和 mongo.exe 工作正常。pymongo 似乎有问题,但我不知道如何解决。
采纳答案by Salvador Dali
All you need is to actually install pymongo
(currently you just have mongo and python, but they do not know how to speak with each other). This page is telling you exactly what to do:
您所需要的只是实际安装pymongo
(目前您只有 mongo 和 python,但它们不知道如何相互交谈)。这个页面告诉你具体该怎么做:
- go to pymongo page
- download and run installer.
- 转到pymongo 页面
- 下载并运行安装程序。
回答by vanduc1102
I am new to Python,
我是 Python 新手,
But I think install setuptoolsis a good idea,
但我认为安装setuptools是个好主意,
after that:
在那之后:
pip install pymongo
回答by user7527153
Try this:
尝试这个:
sudo apt-get install python-pip
sudo pip install pymongo
sudo apt-get install python-pip
sudo pip install pymongo
回答by doer_uvc
If you have installed pymongo using following command :
如果您使用以下命令安装了 pymongo:
sudo pip install pymongo or
sudo -E pip install pymongo
And still you are getting import error then try to run your python script with sudo like :
您仍然遇到导入错误,然后尝试使用 sudo 运行您的 python 脚本,例如:
sudo python example.py
If you are able to run the script this way, but not without sudo. Then there can be a problem with PYTHON_PATH or Permission issue.
如果您能够以这种方式运行脚本,但不能没有 sudo。那么可能存在 PYTHON_PATH 或权限问题。
Solving isssue#1 (i.e. PYTHON_PATH) : Location where pip installs packages and location where python looks for packages do not match.
解决问题#1(即 PYTHON_PATH):pip 安装包的位置和 python 查找包的位置不匹配。
So how do you find where pip install packages ? Run following command :
那么如何找到 pip 安装包的位置呢?运行以下命令:
sudo pip show pymongo
It shows output like this :
它显示如下输出:
---
Name: pymongo
Version: 3.4.0
Location: /usr/local/lib/python2.7/dist-packages
Now you know where pip install packages. Add following line in your .bashrc :
现在您知道 pip 安装包的位置了。在 .bashrc 中添加以下行:
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/dist-packages/
Run following command to execute .bashrc again :
运行以下命令再次执行 .bashrc :
source .bashrc
Now try to run python script without sudo. It should run.
现在尝试在没有 sudo 的情况下运行 python 脚本。它应该运行。
If not then do the following :
如果没有,请执行以下操作:
Solving issue#2 (i.e. Permission): Allow non-root users to read and execute python pacakages.
解决问题#2(即权限):允许非 root 用户读取和执行 python pacakages。
sudo chmod -R ugo+rX /usr/local/lib/python2.7/
This should solve your all problems. You should be able to run python script without sudo.
这应该可以解决您的所有问题。您应该能够在没有 sudo 的情况下运行 python 脚本。
回答by FlyingZipper
- Make a new folder in your documents like "flask-pymongo"
- In your terminal change directory to C:\Users\YOUR_USERNAME\Documents\Flask-PyMongo\
- git clone https://github.com/dcrosta/flask-pymongo
- cd flask-pymongo
- py setup.py develop ORpython setup.py develop (depends how python was install to your path)
- from anywhere in your terminal use : pip install pymongo
- 在您的文档中创建一个新文件夹,例如“flask-pymongo”
- 在您的终端将目录更改为 C:\Users\YOUR_USERNAME\Documents\Flask-PyMongo\
- git 克隆https://github.com/dcrosta/flask-pymongo
- cd 烧瓶-pymongo
- py setup.py develop或python setup.py develop(取决于python如何安装到您的路径)
- 从终端的任何地方使用:pip install pymongo
Solution is for windows users
解决方案适用于 Windows 用户
回答by Developer33
If you have a problem like that probably you didn't two things.
如果你有这样的问题,可能你没有做两件事。
you didn't install pymongo.You can install by below command;
$
pip install pymongo
You installed pymongo but You have two python packages location. like below;
C:\Python\Python37-32\Lib\site-packages\pymongo
(pymongo installed here)C:\Anaconda3\Lib\site-packages\
"pymongo is not here"And you try to work here.
Probably you run Spyder but Spyder is looking to Anaconda\Lib\site-packages\ but pymongo packages are not here.
你没有安装pymongo。你可以通过下面的命令安装;
$
pip install pymongo
您安装了 pymongo 但您有两个 python 包位置。如下图;
C:\Python\Python37-32\Lib\site-packages\pymongo
(这里安装了pymongo)C:\Anaconda3\Lib\site-packages\
“pymongo 不在这里”而你尝试在这里工作。
可能你运行 Spyder,但 Spyder 正在寻找 Anaconda\Lib\site-packages\ 但 pymongo 包不在这里。
Sorry for bad english.
抱歉英语不好。
回答by Egemen Yi?it K?mürcü
I was working on Python 3 but installed the Python 2 version of pymongo with
我正在研究 Python 3,但安装了 Python 2 版本的 pymongo
$ pip install pymongo
command. I uninstalled this version of pymongo with
$ pip install pymongo
命令。我卸载了这个版本的 pymongo
$ pip uninstall pymongo
and installed Python 3 version of it via
$ pip uninstall pymongo
并通过以下方式安装了它的 Python 3 版本
$ pip3 install pymongo
. (after installing pip3 via $ sudo-apt install pip3
in linux terminal). I hope this solves your problem as well as mine.
$ pip3 install pymongo
. (通过$ sudo-apt install pip3
在 linux 终端安装 pip3 之后)。我希望这能解决你和我的问题。
回答by ColossalChris
For me I had this error after I'd installed pymongo via console/terminal but when I looked in my project's interpreter (for example in PyCharm you go to
对我来说,我在通过控制台/终端安装 pymongo 后遇到了这个错误,但是当我查看我的项目的解释器时(例如在 PyCharm 中,你去
Preferences > 'Project: <'name of your project'>' > Project Interpreter
首选项 > '项目:<'您的项目名称'>' > 项目解释器
I saw things like pip and setuptools but not pymongo. I clicked the '+' at the bottom of the pane and searched for pymongo and found it and could install it there. After adding this the project ran fine
我看到了 pip 和 setuptools 之类的东西,但没有看到 pymongo。我单击窗格底部的“+”并搜索 pymongo 并找到它并可以将其安装在那里。添加此项目后,该项目运行良好