Python Application Tuning Guidelines

I know this problem was discussed earlier, but I am trying my best to find a fresh explanation of how to approach the configuration between the local development and production server.

What I have done so far: I had one my_app_config.py file in which there was a section with sections "machine / script" ("test against production"), which I could just comment on. I would develop using hard coding the local machine path, test database, my test pattern table, etc. When the time comes to deploy the code on the server, I will comment on the "test" section and uncomment the "production section". As you can guess, this is due to errors.

I recently used 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') 

There are many problems with this ...

  • I need to import at the beginning of each file
  • Unable to change the file name my_app_config.ini . So, I rely on comments in the contents of the .ini file to find out who I'm dealing with. They are stored in the folder tree, so I know what it is.
  • Please note that the path to the configuration file is indicated here. Thus, depending on where the python file lives in the tree structure, it is determined if I get a copy / paste error.

I tried to set environment variables at the beginning of the program, but all the import for all modules is performed immediately when the code runs. I got "not found" errors on the left and right.

What I want: To understand how to save all configurations stored in one place, which is not easy to lose because of what I am doing. I want an easy way to save these configuration files (ideally a single file or script) under version control (security is another problem, I get distracted). I want to be able to seamlessly switch contexts (local test, local production, serverA-test, serverA-production, serverB-test, serverB-production). In my application is used

  • my_app_config.ini read by my parser
  • uwsgi.ini , read by the emperor of the uwsgi application server.
  • web_config.py used when using a flask
  • nginx.conf , symbolically bound to web server configuration
  • celery configuration

not to mention different paths for everything (ideally processed in a manner of configuration management). I suppose, as soon as I understand this, I will be embarrassed by what has been missing for so long.

Are environment variables what I'm trying to do here?

+6
source share
1 answer

You need to try simple settings . He will solve all your problems. One environment variable defined in one way

in developing

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

in production

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

You can save development.py and production.py not in the repository for security reasons.

Example

Settings / general.py

 SIMPLE_CONF = 'simple' 

app.py

 from simple_settings import settings print(settings.SIMPLE_CONF) 

The documentation lists many other features and benefits.

+1
source

Source: https://habr.com/ru/post/978339/


All Articles