KeyError: 'PROD' when starting run.py

System specs: Windows 10, Anaconda, Python 3.8.5

I have referred to the following tickets:

https://www.mongodb.com/community/forums/t/keyerror-prod-starting-the-mflix/93616/3

https://www.mongodb.com/community/forums/t/run-py-not-working/95322

These are the contents of my dotini_win file which I renamed to .ini:

[PROD]

SECRET_KEY = MYSECRETKEY

MFLIX_DB_URI = mongodb+srv://myuername:mypassword@cluster0.5cpvmqf.mongodb.net/sample_mflix

MFLIX_NS = sample_mflix

[TEST]

SECRET_KEY = MYSECRETKEY

MFLIX_DB_URI = mongodb+srv://myuername:mypassword@cluster0.5cpvmqf.mongodb.net/sample_mflix

MFLIX_NS = sample_mflix

After having set up my virtual environment and finished installing the requirements, I ran the command python run.py but get the following error:

Traceback (most recent call last):

File "run.py", line 13, in <module>

app.config['MFLIX_DB_URI'] = config['PROD']['MFLIX_DB_URI']

File "C:\Users\Username\anaconda3\lib\configparser.py", line 960, in __getitem__

raise KeyError(key)

KeyError: 'PROD'

Please advise as I can’t seem to get past this.

Post a screenshot of the terminal where you ran the command that shows the error you are having.

The context often provides more clues than just the error message.

@steevej

It looks like you have not create or activate your mflix virtual environment. Make sure you install the requirements once it is done.

No @steevej. I had already created and activated it.

Please share the output of executing dir command from the same directory.

I am surprise that after you run the command activate mflix, your prompt still show (base) PS …*. It is supposed to show (mflix) PS ….

But I know not much about python, actually I do not like python at all, they made the same mistake make authors made 30 years later, they should have know better not to make an invisible character important to the syntax.

hey there, I have tested what could be the problem, and it is clearly a step you missed or took a wrong turn there. you get this error because app can’t find .ini (with a leading dot)

Ygo to your m220p folder and copy “dotini_win” to “.ini” :
copy dotini_win .ini

then edit that file with notepad or whatever editor you use.
notepad .ini

@Yilmaz_Durmaz I have already done this prior to running my code. I followed the instructions step by step yet I am facing this issue.

it is clear that configparser package is installed, and is run in the app.py. the problem with it is that it wont show an error if the given config file does not exist.

you can inspect the problem by opening a REPL in that folder (simply go into that folder, open console, “activate” environment, and just type python, in case you forgot).

then copy-paste these lines into it (they are part of run.py).

import os
import configparser
config = configparser.ConfigParser()
config.read(os.path.abspath(os.path.join(".ini")))

if .ini file is not there, then it will print an empty array, [], indicating you messed with the name of the file, or copied it into the wrong folder.

if it is there, you will see ['c:\\Users\\Ganesh\\mflix-python\\.ini'] .
in this case, use this next line to confirm you have PROD in your file:

config['PROD']['MFLIX_DB_URI']

then what you should get is "‘mongodb+srv://…’

and if you get the error in this last step (the same error you get), that would mean you messed inside the ini file. then you need to repeat the copy-edit process for the ini file.

1 Like