任何想法,为什么我会得到“ sys”&quot"不是定义的在Python中创建新的杂种对象之后?
我正在开发一个 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我使用Pymongo 4.xx版本时,我已经经历了这一点,并且通过卸载Pymongo解决了它,然后尝试
pip3 install'pymongo [srv]'
它对我在
'mongodb+srv:// ....'
格式中为我工作。有关Pymongo安装的更多信息
I've experienced this when I use pymongo 4.xx version and I solved it by uninstalling pymongo then I try
pip3 install 'pymongo[srv]'
It works for me for connection string in
'mongodb+srv://....'
format.More info for the pymongo installation pymongo - "dnspython" module must be installed to use mongodb+srv:// URIs
我想我应该更加小心地检查我的依赖关系。我认为 Repl.it 安装了 SparkedHost 没有安装的 pymongo 依赖项。
安装 dnspython 和 Flask 解决了我的问题。
另外,我注意到我忘记了
pokedb = 集群["精灵宝可梦"]
在 getWants 中。
I guess I should have been more careful about checking my dependencies. Repl.it installed pymongo dependencies that SparkedHost didn't, I think.
Installing dnspython and Flask solved my issue.
Also, I noticed I forgot
pokedb = cluster["pokemon"]
in getWants.