发布PyPI包时在包内脚本中读取数据时出错
我在尝试发布一个Pypi包,目的是获取印度的缩写,并把数据以JSON格式保存。我按照Stack Overflow上建议的路径和文件夹结构进行了操作,但仍然遇到同样的问题。这里附上了文件夹结构的图片供参考,点击这里查看图片。
以下是我的代码:
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'
1 个回答
0
看起来你的数据文件没有包含在你的包里。如果你使用的是setuptools,根据文档,你有两种选择:
- 在你的项目根目录下创建一个
MANIFEST.in
文件,内容可以是:
include Abbre/output_2.json
- 在
setup.py
文件中添加以下内容:
package_dir={"": "."},
package_data={"Abbre": ["output_2.json"]},