我使用pymongo连接到mongodb服务器上的数据库。我进行了所有设置,并使用了一个简单的教程来介绍pymongo中的基本知识。我最终将其写入一个python文件:
from pymongo import MongoClient
from random import randint
client = MongoClient("localhost", 27017) #Class from PyMongo module
db = client["rothe_plana"]
# Initialize database settings for employers and events collections:
employersCollect = db["employers"]
eventsCollect = db["events"]
#-----------------------------------------------------
#Employer database managment:
#-----------------------------------------------------
#Inserts passed dictionary objects of employer profiles:
def insertNewEmployer(new_employer_profile):
while True:
try:
readyProfile = new_employer_profile.copy()
readyProfile['employer_id'] = randint(100, 999)
employersCollect.insert_one()
except pymongo.errors.DuplicateKeyError:
continue
break
def getListOfEmployerIDs():
pass #get employer ids to identify and render template elements.
# -----------------------------------------------------
# Events database managment:
# -----------------------------------------------------
#Inserts passed dictionary objects of event data:
def insertNewEvent(new_event_data):
while True:
try:
readyEventData = new_employer_profile.copy()
readyEventData['event_id'] = randint(10000000, 99999999)
employersCollect.insert_one()
except pymongo.errors.DuplicateKeyError:
continue
break
但是如果我运行此命令,则会出现异常:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2018.1.4\helpers\pydev\pydevd.py", line 1664, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm 2018.1.4\helpers\pydev\pydevd.py", line 1658, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm 2018.1.4\helpers\pydev\pydevd.py", line 1068, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2018.1.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/thoma/OneDrive/Projects_For_The_Web/Fliesen Rothe/PlanA/Pyramid_PlanA/pyramid_plana/datadbhandler.py", line 1, in <module>
from pymongo import MongoClient
File "C:\Users\thoma\OneDrive\Projects_For_The_Web\Fliesen Rothe\PlanA\Pyramid_PlanA\venv\lib\site-packages\pymongo\__init__.py", line 77, in <module>
from pymongo.collection import ReturnDocument
File "C:\Users\thoma\OneDrive\Projects_For_The_Web\Fliesen Rothe\PlanA\Pyramid_PlanA\venv\lib\site-packages\pymongo\collection.py", line 29, in <module>
from pymongo import (common,
File "C:\Users\thoma\OneDrive\Projects_For_The_Web\Fliesen Rothe\PlanA\Pyramid_PlanA\venv\lib\site-packages\pymongo\message.py", line 654, in <module>
_op_msg_uncompressed = _cmessage._op_msg
AttributeError: module 'pymongo._cmessage' has no attribute '_op_msg'
由于我当然没有碰过Pymongo模块代码,因此我在上面的代码中做错了什么。网页上也没有显示任何结果,因此对此有明确的解释吗?
编辑: :我仔细查看了上述错误提供的文件。而且我可以看到指定类中的属性确实存在。因此,这很奇怪。即使我从pymongo中注释掉了依赖行,对于同一类也存在另一个AtrributeError。
答案 0 :(得分:0)
我终于解决了这个问题。原来,我的文件系统中的权限未正确处理。
我最初是通过PyCharm(pip install pymongo
)安装PyMongo的。但这是行不通的(不知道为什么),但是我终于从虚拟环境中卸载了pymongo并通过虚拟环境中的PowerShell再次手动安装了它:
python -m pip install pymongo
重新启动PyCharm并运行项目并没有出现任何错误。希望这可以帮助其他人解决这个问题