简体   繁体   中英

Best way to load a file (not a python module) relative to sys.path

I'd like to load a file from somewhere in the python path ( sys.path ), in a similar way as you can do in java with Class.getResourceAsStream() . This file is not a python module, but I happen to (ab)use python packaging for a repo that basically contains a collection of JSON files.

So, to be clear, the packaged data contains no python except for a setup.py file. It's an egg with no python inside. The project that uses the packaged data is the one with python code.

I use pip install -e <the git URL> to install the package. It ends up in my virtual environment at <env_path>/src/<repo> . This location appears as one of the entries in sys.path .

Is there a simple-ish library call that lets me find the absolute path to one such file, without me having to search for the file by looping through sys.path ?

Something like:

path = find_in_sys_path('campaigns/get.json')

...whatever find_in_sys_path should actually be called.

Checkout setuptools https://pythonhosted.org/setuptools/pkg_resources.html#resourcemanager-api

import pkg_resources
my_data = pkg_resources.resource_string(__name__, "campaigns/get.json")

After reading @larsmans comment , I realised that although my egg-packaged data contains no python module that I can use as a reference to load a resource, I can easily locate its absolute path from sys.path with something like:

path = filter(lambda x: 'rbx-api-mocks' in x, sys.path)[0]
# rbx-api-mocks is the "egg name"

That I can later use to build the path to the resource with:

resource = os.path.join(path, 'campaigns', 'campaign', 'query.json')

I will probably end up going for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM