postgresqlsqlalchemypyramidinikotti

How can I use a system environment variable inside a pyramid ini file?


I exported a variable called DBURL='postgresql://string'and I want to use it in my configuration ini file, e.g::

[app:kotti]
sqlalchemy.url = %(DBURL)s

That's not working.


Solution

  • Put this in your __init__.py:

    def expandvars_dict(settings):
        """Expands all environment variables in a settings dictionary."""
        return dict((key, os.path.expandvars(value)) for
                    key, value in settings.items())
    

    Then when you export an environment variable to your shell, the proper syntax is this:

    sqlalchemy.url = ${DBURL}
    

    Once you have that environment variable set within your .ini, then you can use the configparser syntax:

    sqlalchemy.connection = %(sqlalchemy.url)s%(user:pass and other stuff)s
    

    Idea stolen from https://stackoverflow.com/a/16446566/2214933