我试图发布一个 Pypi 包来检索印度缩写并以 JSON 格式获取数据。还按照堆栈溢出中的建议给路径提供了文件夹结构,但仍然面临同样的问题。附件是文件夹结构和参考的图像 代码如下
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
附上错误供您参考
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'
您的包中似乎不包含数据文件。如果你使用setuptools,根据文档,你有2个选择:
MANIFEST.in
文件,例如:include Abbre/output_2.json
setup.py
: package_dir={"": "Abbre"},
package_data={"Abbre": ["output_2.json"]},