当前位置: 动力学知识库 > 问答 > 编程问答 >

Python app configuration best practices

问题描述:

I know this issue has been discussed before, but I am struggling to find a starightforward explanation of how to approach configuration between local development and production server.

What I have done so far: I had one my_app_config.py file that had a section with machine / scenario (test vs production) sections I could just comment out. I would develop with my local machine path hardcoded, test database connection string, my test spreadsheet location, etc. When it comes time to deploy the code to the server, I comment out the "test" section and uncomment the "production section". As you may guess, this is wrought with errors.

I recently adopted the Python ConfigParser library to use .ini files. Now, I have the following lines in my code

import ConfigParser

config = ConfigParser.RawConfigParser()

config.read(os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'settings',

'my_app_config.ini')))

database_connect_string_admin = config.get('Database', 'admin_str')

The problems with this are many...

  1. I need to have the import at the top of every file
  2. The filename my_app_config.ini can't change. So, I rely on comments within the content of the .ini file to know which one I'm dealing with. They are stored in a folder tree so I know which is which.
  3. notice the path to the config file is defined here. So, depending where the python file lives in the tree structure dictates if I get a copy / paste error.

I tried to set environment variables at the beginning of the program, but all the imports for all modules are performed right away at code launch. I was getting "not found" errors left and right.

What I want: To understand how to keep all the configurations stored in one place that is not easy to lose track of what I am doing. I want an easy way to keep these configuration files (ideally one file or script) under version control (security is a whole other issue, I digress). I want to be able to seamlessly switch contexts (local-test, local-production, serverA-test, serverA-production, serverB-test, serverB-production) My app uses

  1. my_app_config.ini read by my parser
  2. uwsgi.ini read by the uwsgi application server emperor
  3. web_config.py used by the flask application
  4. nginx.conf symlinked to the web server's configuration
  5. celery configuration

not to mention different paths for everything (ideally handled within the magic config handling genie). I imagine once I figure this out I will be embarrassed it took so long to grasp.

Are Environment variables what I am trying to do here?

网友答案:

You have to try simple settings. It will resolve all you issues. One way set environment variable

in development

$ export SIMPLE_SETTINGS=settings.general,settings.development
$ python app.py

in production

$ export SIMPLE_SETTINGS=settings.general,settings.production
$ python app.py

You can keep development.py and production.py not in a repository for security reasons.

Example

settings/general.py

SIMPLE_CONF = 'simple'

app.py

from simple_settings import settings

print(settings.SIMPLE_CONF)

The documentation indicated many more features and benefits.

分享给朋友:
您可能感兴趣的文章:
随机阅读: