gpt4 book ai didi

python - 知道为什么在 Python 中创建新的 MongoClient 对象后我会得到 "name ' sys' is not defined”吗?

转载 作者:行者123 更新时间:2023-12-05 01:24:01 31 4
gpt4 key购买 nike

我正在开发一个 Discord 机器人,它可以响应一些用户命令将一些数据保存到 mongodb 数据库中。奇怪的是它适用于 repl.it,但我在 SparkedHost 上收到此错误。创建新的 MongoClient 对象时出现奇怪的错误。服务器安装了 Python3 3.6.15。追溯看起来像这样:

Traceback (most recent call last):
File "/home/container/.local/lib/python3.6/site-packages/discord/ext/commands/core.py",
line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/home/container/main.py", line 36, in listMyWants
await botcommandscontroller.listWants(ctx, ctx.author.id)
File "/home/container/botcommandscontroller.py", line 10, in listWants
wants = mongodbcontroller.getWants(targetID)
File "/home/container/mongodbcontroller.py", line 17, in getWants
cluster = MongoClient(os.getenv('MONGOCONNECT'))
File "/home/container/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line
712, in __init__
srv_max_hosts=srv_max_hosts,
File "/home/container/.local/lib/python3.6/site-packages/pymongo/uri_parser.py", line
467, in parse_uri
python_path = sys.executable or "python"
NameError: name 'sys' is not defined

这是我的 getWants 方法:

def getWants(userID):
load_dotenv()
cluster = MongoClient(os.getenv('MONGOCONNECT'))
wantcollection = pokedb["wants"]
userWants = ""
pipeline = [{'$lookup':
{'from': 'pokemon',
'localField': 'dexnum',
'foreignField': 'NUMBER',
'as': 'userwants'}},
{'$unwind': '$userwants'},
{'$match':
{'discord_id': userID}}]
for doc in (wantcollection.aggregate(pipeline)):
if doc['shiny']:
userWants += "shiny "
userWants += doc['userwants']['NAME'] + ", "
if len(userWants) > 2:
userWants = userWants[0:len(userWants) - 2]
return userWants

此方法可能没有任何相关信息,但这里是 listWants:

async def listWants(ctx, targetID):
if targetID is None:
await ctx.send(Constants.ErrorMessages.NO_USER_FOUND)
return
wants = mongodbcontroller.getWants(targetID)
if wants != "":
await ctx.send(wants)
else:
await ctx.send(Constants.ErrorMessages.NO_WANTS_FOUND)

最佳答案

我在使用 pymongo 4.xx 版本时遇到过这个问题,我通过卸载 pymongo 解决了这个问题,然后我尝试pip3 安装 'pymongo[srv]'

它适用于 'mongodb+srv://....' 格式的连接字符串。

有关 pymongo 安装的更多信息 pymongo - "dnspython" module must be installed to use mongodb+srv:// URIs

关于python - 知道为什么在 Python 中创建新的 MongoClient 对象后我会得到 "name ' sys' is not defined”吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71759462/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com