Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python dict.get() raises KeyError

I am getting lost here, Python 2.7, I have a dictionary mt, and I use the get() method, which by documentation says:

get(key[, default]) Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

but I still get

 File "/home/ubuntu/subscription-workers/commands/dr/rebilling.py", line 48, in rebill
    if mt.get('is_rebill', 0) == 1:
 KeyError: 'is_rebill'

Any ideas why?

The mt is a normal dict, that sometimes does not have the key.

like image 350
Belda Avatar asked Nov 11 '14 09:11

Belda


1 Answers

So I nailed the problem down. Before this code was put in place there was this one

File "/home/ubuntu/subscription-workers/commands/dr/rebilling.py", line 48, in rebill
    if mt['is_rebill'] == 1:
KeyError: 'is_rebill'

The problem was that there were .pyc files from the older version, but the stack trace was loading the actual code. After running

find . -name "*.pyc" -exec rm -rf {} \;

and reloading the app everything was fine and without problems.

like image 126
Belda Avatar answered Oct 01 '22 09:10

Belda