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)
2
Answers
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.
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