pythonpyramidbuildoutpaster

How do I access ${buildout:directory} from Python code?


I have a Pyramid web application managed with zc.buildout. In it, I need to read a file on disk, which is located in a sub-directory of buildout directory.

The problem is with determining the path to the file - I do not want to hard-code the absolute path and just providing a relative path does not work when serving the app in production (supposedly because the working directory is different).

So the promising "hooks" I am thinking about are:


Solution

  • If the path to the file relative to the buildout root or location of paster.ini is always the same, which it seems it is from your question, you could set it in paster.ini:

    [app:main]
    ...
    config_file = %(here)s/path/to/file.txt
    

    Then access it from the registry as in Reinout's answer:

    def your_view(request):
        config_file = request.registry.settings['config_file']