1

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.

b4oshany
  • 692
  • 1
  • 7
  • 15

2 Answers2

2

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

Steve Piercy
  • 13,693
  • 1
  • 44
  • 57
  • @b4oshany Sorry, I forgot to include the function that loads settings, so I edited my answer. It's a third option to what Michael Merickel suggested. – Steve Piercy May 10 '18 at 04:49
1

PasteDeploy (the ini format pyramid is using here) does not support reading directly from environment variables. A couple common options are:

1) Set that option yourself in your main.

import os

def main(global_config, **settings):
    settings['sqlalchemy.url'] = os.environ['DBURL']
    config = Configurator(settings=settings)
    ...

2) Define your ini file as a jinja2 template and have a command to render it out to ini format, and just run that as part of your deploy process.

Michael Merickel
  • 23,153
  • 3
  • 54
  • 70