任何想法,为什么我会得到“ sys”&quot"不是定义的在Python中创建新的杂种对象之后?

发布于 2025-01-19 14:49:22 字数 2159 浏览 4 评论 0 原文

我正在开发一个 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)

I'm working on a Discord bot that saves some data to a mongodb database in response to some user commands. The weird thing is it works on repl.it, but I get this error on SparkedHost. I'm getting an odd error when I create a new MongoClient Object. The server has Python3 3.6.15 installed. The traceback looks like this:

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

Here's my getWants method:

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

This method probably doesn't have any relevant info, but here's 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女 2025-01-26 14:49:22

当我使用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

再浓的妆也掩不了殇 2025-01-26 14:49:22

我想我应该更加小心地检查我的依赖关系。我认为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文