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
config = ConfigParser.RawConfigParser()
config.read(os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'settings',
database_connect_string_admin = config.get('Database', 'admin_str')
The problems with this are many...
my_app_config.inican't change. So, I rely on comments within the content of the
.inifile to know which one I'm dealing with. They are stored in a folder tree so I know which is which.
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
my_app_config.iniread by my parser
uwsgi.iniread by the uwsgi application server emperor
web_config.pyused by the flask application
nginx.confsymlinked to the web server's 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
$ export SIMPLE_SETTINGS=settings.general,settings.development $ python app.py
$ 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.
SIMPLE_CONF = 'simple'
from simple_settings import settings print(settings.SIMPLE_CONF)
The documentation indicated many more features and benefits.