1

I have created a .whl package using python3.9 setup.py bdist_wheel and also I have checked that all files gets included in this .whl file. Below is the .whl file structure:

MyPackage/
- api/
   - workflow
      - cfg       
         - data1.json
         - data2.json
         - data3.json
- scripts.py

and I am trying to read json files present inside api/workflow/cfg and add it into dictionary, using below function from scripts.py:

def read_cfg(storage_dir="api/workflow/cfg") -> Dict[str, wd]:
    wf: Dict[str, wd] = {}
    for json_file in Path(storage_dir).glob("*.json"):
        with open(json_file) as f:
            definition = json.load(f)
            wf[definition["name"]] = wd(**definition)
    return wf

However, it gives an empty dictionary. What am I doing wrong and what needs to be changed to read the json files using the .whl file?

This runs successfully when I try to run it directly without using the .whl file.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79
  • Does this answer your question? [How to read a (static) file from inside a Python package?](https://stackoverflow.com/questions/6028000/how-to-read-a-static-file-from-inside-a-python-package) – sinoroc Dec 12 '22 at 10:51
  • @sinoroc, no that is different but thank you I have already posted my answer as a solution – Rakesh Shetty Dec 13 '22 at 06:16

1 Answers1

-1

I found the solution, I have used relative path where all JSON file are present. As JSON files not exist as an actual file on the system when using running project as package.

So used - storage_dir = os.path.join(os.path.dirname(__file__), "cfg")

def read_cfg(storage_dir = os.path.join(os.path.dirname(__file__), "cfg")) -> Dict[str, wd]:
    wf: Dict[str, wd] = {}
    for json_file in Path(storage_dir).glob("*.json"):
        with open(json_file) as f:
            definition = json.load(f)
            wf[definition["name"]] = wd(**definition)
    return wf
Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79