swizzard
swizzard

Reputation: 1095

pattern for consistent paths to files in python

Given a project structure like this:

my_project/
    __init__.py
    config.json
    utils/
        __init__.py
        config_manager.py
    foo/
        __init__.py
        foo.py
        bar/
            __init__.py
            bar.py
        baz/
            __init__.py
            baz.py
            quux/
                __init__.py
                quux.py

I've got a function in config_manager.py that takes an optional path to a config file, but should default to use my_project/config.json. That function should be the only way to interact with the config--call it, get the config, pull out whatever's needed. I'd like to be able to run code using the config from anywhere, e.g., not just from the top-level directory, and have it "just work".

I can hack out something like

# config_manager.py
def get_config(config_path=None):
    if config_path is None:
        cdir = os.path.dirname(__file__)
        config_path = os.path.join(cdir, '..', 'config.json')
        ...

but that's brittle (what if the location of config.json or config_manager.py changes?) and ugly. Any patterns/tips/suggestions/practices to handle this kind of situation?

(I'm sure I'm not the first person to wonder about this, it's just kind of hard to google.)

Upvotes: 0

Views: 211

Answers (2)

Vor
Vor

Reputation: 35149

My suggestion on that would be package everything you need. For example you can add something like this to setup.py

...
package_data={'mypackage': ['*.json']},
...

Then in your code you will be able to do access config like that:

from pkg_resources import resource_stream
config_file = resource_stream(__name__, 'config.json')

Upvotes: 1

Prune
Prune

Reputation: 77860

This is why we maintain PATH variables in most operating systems: to tell us where we should search for things. Look up the path function; it may be what you need.

Depending on the user model you have in mind (not entirely clear from your posting), you may want to employ os.walk() to search for a file under a given directory.

Upvotes: 0

Related Questions