Search code examples
pythonpython-3.xwindows-10ubuntu-16.04keyerror

KeyError on Linux but not Windows


I have a bizarre situation:

class SomeClass(object):
    def __init__(self, data):
        self.data = {}
        self.data = data
        old_data_name = "SOURCE" # self.data[old_data_name] = a list of values
        try:
           self.data[old_data_name] = [1,2,3,4]
        except Exception as e:
           print(str(e))
        new_data_name = "NEW" # a name
        self.data[new_data_name] = numpy.mean(self.data[old_data_name])

So this code works perfectly on windows - I have debugged this statement by statement and verified it. When I deploy my code on to a linux server. It throws me a KeyError: old_data_name

I am sure the data is coming in to the point where the key error should not occur. Why does python behave so differently on Linux and Windows?


Solution

  • My first impression was a version mismatch:

    @Basic Thanks to Basic The issue was indeed version mismatch. Visual Studio on windows defaulted to 3.6 and Ubuntu was forcing 3.5.2 Din't know there were such huge breaking changes just from a small revision from python. Seems like a big mess.

    But upon further investigation. It revealed that version python 3.5.2 has weird way of reading json files compared to 3.6.6 Essentially the way I loaded my data meant that it self references properties that once loaded. for eg:

    {properties: {"stuff": "do this", "stuff2": "do another with @stuff" }}
    

    My code was looking for stuff in stuff2 when stuff was never loaded

    Python version 3.5.2 has no specific order in which it loads json data to dicts which causes issues.

    Python 3.6.6 fixes this.