Python module database configuration?
I have a python module which contains a few objects, one of which uses a MySQL connection to persist some data. What's the best way to allow for easy configuration o开发者_运维百科f the MySQL connection information without making the user go into the installed module location and edit files?
Allow the user to write configuration information for the program in a file of the format that ConfigParser knows how to parse -- this way, the user doesn't have to "go into the installed module location" but can edit the configuration file in more convenient places.
It is traditional and helpful for the program to attempt to read both a "per-user" configuration file (in the user's home directory or subdir thereof), and a "per-location" configuration file that a system administrator could use to provide the users with some defaults; so, the read method of config parser objects accepts a list of filenames and tries to parse each of them in sequence (see the simple example in the docs I'm pointing to).
You could pull the configuration details for MySQL from an .ini
file using ConfigParser.
精彩评论