Best way to store JSON data in a Python Module
Question: I have a Python module I'm developing that's includes a big JSON file as part
of the data it relies on. I want Python users to be able to import the JSON
file as a Python variable and for users of other programming languages, to be
able to use the JSON file directly.

So, what I'm trying to figure out is what's the best way to make the JSON
object "importable". Right now, my solution is in `__init__.py`:

    
    
    import json
    import os
    
    with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'seals.json')) as f:
        seals_data = json.load(f)
    

Then Python devs can call:

    
    
    from my_module import seals_data
    

And it more or less works, but this feels weird to me, and I want to make sure
there isn't a cleaner way to make the json importable.


Answer: Your option is probably the best way to store the data as a python structure
in a module for someone to use. The only down side I see with it is anything
that imports that module will have to wait for it to do IO, which is not
ideal, esp if the module is ever used for things that do not involved that
data.

I would use a class, or function to lazily load that data, allowing that IO
hit to be offset to only when that data is requested.

Here is an example:

    
    
    SEALS_DATA = None
    def get_seals_data():
        global SEALS_DATA
        if SEALS_DATA is None:
            SEALS_DATA = json.load(open("%s/seals.json"%(os.path.dirname(__file__))))
        return SEALS_DATA
    

After they import the module they will have no deal until the first time they
call get_seals_data(). Any call after that should already have the data
loaded.

