You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

发布PyPI包时读取包内JSON数据遇FileNotFoundError问题求助

问题:PyPI包中JSON文件找不到错误

我正在开发一个用于查询印度缩写的PyPI包,数据以JSON格式存储。已经按照Stack Overflow的建议配置了文件夹结构和路径,但还是碰到了文件找不到的错误。代码里我本来想用pkg_resources来获取资源路径,但实际报错显示代码里直接用了相对路径Abbre\output_2.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("暂无对应数据")
        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         示例输出:[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'

内容的提问来源于stack exchange,提问作者Richard Alphonse

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 11:19:54