PyMongo:类_cmessage中现有属性的AttributeErrors
Posted
技术标签:
【中文标题】PyMongo:类_cmessage中现有属性的AttributeErrors【英文标题】:PyMongo: AttributeErrors for existing attributes in class _cmessage 【发布时间】:2018-07-03 07:01:04 【问题描述】:我使用 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。
【问题讨论】:
【参考方案1】:我终于解决了这个问题。事实证明,我的文件系统中的权限没有得到正确处理。
我最初是通过 PyCharm (pip install pymongo
) 安装 PyMongo。但这不起作用(不知道为什么),但我最终从虚拟环境中卸载了 pymongo,并在虚拟环境中通过 PowerShell 再次手动安装:
python -m pip install pymongo
重新启动 PyCharm 并运行项目确实不再出现错误。希望这可以帮助其他人解决这个问题
【讨论】:
以上是关于PyMongo:类_cmessage中现有属性的AttributeErrors的主要内容,如果未能解决你的问题,请参考以下文章