Publishing an Pypi Package getting an error in Read the Data within Package Script

30 Views Asked by At

I was trying to publish a Pypi Package to retrieve the Indian Abbreviation and have the data in the JSON format. Also given the path an folder structure as suggested in Stack overflow but still facing the same issue. Attached is the image for the reference of folder Structure and enter image description here The code follows as

json_file_path=pkg_resources.resource_filename('Abbre','my_data/output_2.json')
    with open (json_file_path,'r')as f:
        loaded_dict=json.load(f)
        arr1=arr.lower().strip()
        #print(loaded_dict)

    empty_list=[]
    for i,j in loaded_dict.items():
        if i==arr1:
                empty_list.append(j)
                print(empty_list)
                break

        else:
            print(" The data is not available")
            break

Attached the error for your reference

FileNotFoundError                         Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_54872\4275681862.py in <module>
      1 import Abbre
----> 2 Abbre.Abbreviation('nlc')

~\anaconda3\lib\site-packages\Abbre.py in Abbreviation(arr)
      6         will give an output of list Life Insurance of corporation
      7         """
----> 8     with open ('Abbre\output_2.json','r')as f:
      9         loaded_dict=json.load(f)
     10         arr1=arr.lower().strip()

FileNotFoundError: [Errno 2] No such file or directory: 'Abbre\\output_2.json'
1

There are 1 best solutions below

3
jayvynl On

It seems that data file is not included in your package. If you use setuptools, according to the document, you have 2 choices:

  1. create a MANIFEST.in file in your project root like:
include Abbre/output_2.json
  1. add this to setup.py:
    package_dir={"": "."},
    package_data={"Abbre": ["output_2.json"]},