setuptools pyproject.toml - managing paths of enclosed config files

37 views Asked by At

When using pyproject.toml setuptools how can I dynamically assure that my binary finds its config enclosed in the same directory as the python code?

After pip install files are located:

:
c:\dist\venvs\ranchercli\lib\site-packages\ranchercli\ranchercli-projects.json
c:\dist\venvs\ranchercli\lib\site-packages\ranchercli\ranchercli.py
c:\dist\venvs\ranchercli\scripts\ranchercli.exe
:

But when I run:

# same dir:
ranchercli.exe --ns='keycloak' --env=prod --refresh
20240322095209.115|ERROR|C:\dist\venvs\ranchercli\Lib\site-packages\ranchercli\ranchercli.py:89|--projectfile=./ranchercli-projects.json not found
# relative:
20240322101246.345|ERROR|C:\dist\venvs\ranchercli\Lib\site-packages\ranchercli\ranchercli.py:89|--projectfile=../lib/site-packages/ranchercli/ranchercli-projects.json not found

I only got full path working but the path changes with any install:

C:\\dist\\venvs\\ranchercli\\Lib\\site-packages\\ranchercli\\ranchercli-projects.json

2

There are 2 answers

0
MortenB On

A workaround like @phd suggests, is to get the path from __file__ with pathlib

parser.add_argument('--projectfile', help="json projectlist file", default='./ranchercli-projects.json')

:
if not os.path.isfile(args.projectfile):
  if args.projectfile.startswith('.'):
    args.projectfile = pathlib.Path(__file__).parent.absolute() / pathlib.Path(args.projectfile)
    if not os.path.isfile(args.projectfile):
      log.error(f"--projectfile={args.projectfile}, not found")
      sys.exit(-1)

But I need to do this for all files, Would like to have a generic way in setuptools.

0
Anderson Bravalheri On

I am not sure if I understood how this specifically related to setuptools as this seems to be a runtime behaviour (setuptools deals with build-time behaviour).

Possibly importlib.resources (and its backfill importlib_resources) can help here.

The function importlib_resources.files("<NAME OF YOUR ROOT PACKAGE>") should allow you to traverse all the files inside of your package installation.