使用pkg_资源在python包的当前发行版中加载JSON文件

使用pkg_资源在python包的当前发行版中加载JSON文件,python,json,packages,Python,Json,Packages,我正在构建一个具有以下目录结构的包- terraai_preprocessing |setup.py ||MANIFEST.in |terraai_preprocessing |__init__.py |combinatorics |preprocessing |__init__.py |config.json |pp_main.py |pp_helpers.py 我正试图使用pkg_资源将config.json加载到pp_helpers.py中,正

我正在构建一个具有以下目录结构的包-

terraai_preprocessing
|setup.py
||MANIFEST.in
|terraai_preprocessing
  |__init__.py
  |combinatorics
  |preprocessing
    |__init__.py
    |config.json
    |pp_main.py
    |pp_helpers.py
我正试图使用pkg_资源将config.json加载到pp_helpers.py中,正如在其他类似问题中提到的那样。我确信该文件存在,因为-

>>print resource_exists('terraai_preprocessing.preprocessing', 'config.json')
>>True
我尝试过使用以下方法,但最终出现了错误-

>>with open(resource_filename('terraai_preprocessing.preprocessing', 'config.json'),'r') as f:
    config = json.load(f)
ValueError: No JSON object could be decoded

 >>config = json.loads(resource_filename('terraai_preprocessing.preprocessing', 'config.json'))
ValueError: No JSON object could be decoded

 >>config = json.loads(resource_stream('terraai_preprocessing.preprocessing', 'config.json'))
enter code here

 >>config = json.loads(resource_string('terraai_preprocessing.preprocessing', 'config.json'))
ValueError: No JSON object could be decoded
我做错了什么

应该是这样的:

with open('data.json', encoding='utf-8') as data_file:
    data = json.loads(data_file.read())
那么你的情况呢

 config = json.loads(f.read())
它必须是这样的:

with open('data.json', encoding='utf-8') as data_file:
    data = json.loads(data_file.read())
那么你的情况呢

 config = json.loads(f.read())

Python找不到该文件,因为函数
resource\u filename()
返回的地址以
\
作为分隔符。通过将
\\
替换为
/
,它能够找到该文件

所以我使用了以下代码-

>> file_location = resource_filename('terraai_preprocessing.preprocessing','config.json').replace('\\',"/")

>> with open(file_location,'r') as f:
      config = json.load(f)

Python找不到该文件,因为函数
resource\u filename()
返回的地址以
\
作为分隔符。通过将
\\
替换为
/
,它能够找到该文件

所以我使用了以下代码-

>> file_location = resource_filename('terraai_preprocessing.preprocessing','config.json').replace('\\',"/")

>> with open(file_location,'r') as f:
      config = json.load(f)

不,这不起作用,我得到一个没有这样的文件或目录错误。由于config.json是一个包资源,我认为我不能像那样直接访问它。用config.json替换data.json,如果您的文件位于同一目录中,请提供路径/config.json或根目录的绝对路径是的,我确实正确地替换了文件名,并提供了正确的路径。使用-
resource\u filename('terraai\u preprocessing.preprocessing','config.json')
尝试绝对路径,但它仍然抛出
IOError:[Errno 2]没有这样的文件或目录:'./config.json'
运行pwd并验证您在哪里,然后将pwd的输出结束到“/config.json”-/your find path/config.json。pwd输出到
C:\Windows\system32
,因此文件不在那里。我尝试使用
resource\u filename('terraai\u preprocessing.preprocessing','config.json')获取完整路径。
路径是正确的,但是
config=json.load(f)
导致
ValueError:无法解码json对象
不,这不起作用,我没有收到这样的文件或目录错误。由于config.json是一个包资源,我认为我不能像那样直接访问它。用config.json替换data.json,如果您的文件位于同一目录中,请提供路径/config.json或根目录的绝对路径是的,我确实正确地替换了文件名,并提供了正确的路径。使用-
resource\u filename('terraai\u preprocessing.preprocessing','config.json')
尝试绝对路径,但它仍然抛出
IOError:[Errno 2]没有这样的文件或目录:'./config.json'
运行pwd并验证您在哪里,然后将pwd的输出结束到“/config.json”-/your find path/config.json。pwd输出到
C:\Windows\system32
,因此文件不在那里。我尝试使用
resource\u filename('terraai\u preprocessing.preprocessing','config.json')获取完整路径。
路径正确,但
config=json.load(f)
导致
ValueError:无法解码任何json对象