Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 317
Brian Leach Avatar asked Nov 18 '14 16:11

Brian Leach


People also ask

How do you write configuration in Python?

Python Configuration File The simplest way to write configuration files is to simply write a separate file that contains Python code. You might want to call it something like databaseconfig.py . Then you could add the line *config.py to your . gitignore file to avoid uploading it accidentally.

Does Python have a config file?

Python has a standard configuration library called “configparser”, which is very convenient and powerful, but does not address many common problems.

What should I name my Python config file?

I would probably use something like appName. cfg, where appName identifies your application, or the part of the application the configuration is for. Save this answer.


1 Answers

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.pyandproduction.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.

like image 141
fedorshishi Avatar answered Sep 27 '22 17:09

fedorshishi