简体   繁体   中英

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, eg::

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

That's not working.

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

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.

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