Home > Mobile >  Including JSON files as part of Python package
Including JSON files as part of Python package

Time:02-08

I`m writing a package with a "local database" consisting of a folder and subfolders with json files. My database.py module imports these files with a relative path to them (DB_PATH). When installing the package, load fails since files are not found. Is there a way to import those files as part of the package, or a standard way to handle cases like this?

My python code database.py:

DB_PATH = "./db"


def getIndex():
    filename = f"{DB_PATH}/index.json"

    with open(filename) as f:
        data = json.load(f)

    return data

File system:

           
  •  Tags:  
  • Related