Error Running run.py

Here’s what I am trying to do - I have changed the config to point to TEST from PROD and my URLs are the same. I created a secret key as well.

PS C:\Users\tedbo\OneDrive\Documents\mflix-python> python run.py
Traceback (most recent call last):
File “run.py”, line 13, in
app.config[‘MFLIX_DB_URI’] = config[‘PROD’][‘MFLIX_DB_URI’]
File “C:\ProgramData\Anaconda3\lib\configparser.py”, line 958, in getitem
raise KeyError(key)
KeyError: ‘PROD’
PS C:\Users\tedbo\OneDrive\Documents\mflix-python> python run.py
Traceback (most recent call last):
File “run.py”, line 13, in
app.config[‘MFLIX_DB_URI’] = config[‘TEST’][‘MFLIX_DB_URI’]
File “C:\ProgramData\Anaconda3\lib\configparser.py”, line 958, in getitem
raise KeyError(key)
KeyError: ‘TEST’

Please help. What within the INI needs to exist - the instructions are kind of cloudy on that.

Here’s the INI file. I am running this from the location of the python file.

Ticket: Connection

Rename this file to .ini after filling in your MFLIX_DB_URI and your SECRET_KEY

Do not surround the URI with quotes

[PROD]
SECRET_KEY = WDdUO5iE2inmrJZ4QRRt+6EUEo2+cGBSKYmtNYOf5yAR
MFLIX_DB_URI = mongodb+srv://m220student:m220password@mflix-jwtvw.mongodb.net/test?retryWrites=true&w=majority
MFLIX_NS = sample_mflix

[TEST]
SECRET_KEY = WDdUO5iE2inmrJZ4QRRt+6EUEo2+cGBSKYmtNYOf5yAR
MFLIX_DB_URI = mongodb+srv://m220student:m220password@mflix-jwtvw.mongodb.net/test?retryWrites=true&w=majority
MFLIX_NS = sample_mflix

I really need help with this. I have tried what others have done in other threads and nothing works. I can connect via MongoShell and Compass - so obviously the problem lies within the Anaconda environment in some way. I have all the packages available and nothing works. Please help.

I have more information. I found a pymongo sample to create a DB connection and I ran it in PyCharm and it works.

Code:

import pymongo

uri = “mongodb+srv://m220student:m220password@mflix-jwtvw.mongodb.net”

client = pymongo.MongoClient(uri, connectTimeoutMS=200, retryWrites=True)

print(client.stats)

db = client.sample_mflix
print(db.list_collection_names())

Results:

C:\ProgramData\Anaconda3\python.exe “D:/Program Files/mflix-python/TestConnection.py”
Database(MongoClient(host=[‘mflix-shard-00-00-jwtvw.mongodb.net:27017’, ‘mflix-shard-00-02-jwtvw.mongodb.net:27017’, ‘mflix-shard-00-01-jwtvw.mongodb.net:27017’], document_class=dict, tz_aware=False, connect=True, authsource=‘admin’, replicaset=‘mflix-shard-0’, ssl=True, connecttimeoutms=200, retrywrites=True), ‘stats’)
[‘theaters’, ‘users’, ‘sessions’, ‘comments’, ‘movies’]

Process finished with exit code 0

Also - I tried using that exact URI in the URI path within the INI file in past attempts and it fails.

Please respond. I cannot move forward on this course without some real help.

I ran using the most recent INI entry I had for URI and it works using pymongo yet fails in command with an active mflix environment.

import pymongo

uri = “mongodb+srv://m220student:m220password@mflix-jwtvw.mongodb.net/test?retryWrites=true&w=majority”

client = pymongo.MongoClient(uri, connectTimeoutMS=200, retryWrites=True)

print(client.stats)

db = client.sample_mflix
print(db.list_collection_names())

C:\ProgramData\Anaconda3\python.exe “D:/Program Files/mflix-python/TestConnection.py”
Database(MongoClient(host=[‘mflix-shard-00-01-jwtvw.mongodb.net:27017’, ‘mflix-shard-00-02-jwtvw.mongodb.net:27017’, ‘mflix-shard-00-00-jwtvw.mongodb.net:27017’], document_class=dict, tz_aware=False, connect=True, authsource=‘admin’, replicaset=‘mflix-shard-0’, ssl=True, retrywrites=True, w=‘majority’, connecttimeoutms=200), ‘stats’)
[‘theaters’, ‘users’, ‘sessions’, ‘comments’, ‘movies’]

Process finished with exit code 0

I see you write many times INI file. However the instructions specify the .ini file. A dot followed with ini in lowercase. I do not know i this is the problem but I just want to make sure the right file is used before digging deeper.

I wrote that because I love writing in CAPS - however it’s still an *.ini file.

From your screen shot I see that the file has .ini extension. I can also see that the base name is dotini_win. However, the instructions are

2 Likes

TY it worked. I’ve never worked with unnamed ini files before - or any for that matter. TY for the help!