0

thanks for reading the Q.

I've come across SO post where one solution discusses using pkg_resources which I'm trying to use.

My file structure

x
  - x
    - to_read.json
    - __init__.py
  - to_read.json
  - __init__.py 

Yes, I duplicated it. under the x folder as well

My setup file

from setuptools import find_packages, setup

setup(
    name='x',
    version='0.160',
    packages=find_packages(exclude="tests/"),
    url="git@github.com:A/B.git",
    package_data={'x': [
        'to_read.json',
        "x/to_read.json"
    ]},
    install_requires=["foo", "bar"]
)

Code attempting to read it

  1. Error: Exception: the 'package' argument is required to perform a relative import for '.'
return json.loads(str(pkg_resources.open_text(".", 'to_read')))  # note, I removed the extension as mentioned in the solution
  1. FileNotFoundError
return json.loads(str(pkg_resources.open_text("x", 'to_read')))  # note, I removed the extension as mentioned in the solution

I figured that I wouldn't have to import anything (the SO post imported "template") since it's sitting at the top level of my directory?

Thanks!

IanQ
  • 1,831
  • 5
  • 20
  • 29

1 Answers1

0

The solution was

  1. Import the library .... import mylib

with pkg_resources.open_text(mylib, 'to_read.json') as file:
    return json.load(file)

So basically everything that I thought shouldn't have applied did apply. RIP

IanQ
  • 1,831
  • 5
  • 20
  • 29