2

What would be a neat way to share configuration parameters\settings\constants between various projects in Python?

Using a DB seems like an overkill. Using a file raises the question of which project should host the file in its source control...

I'm open for suggestions :)

UPDATE:
For clarification - assume the various projects are deployed differently on different systems. In some cases in different directories, in other cases some of the projects are there and some are not.

Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
  • I just +1'ed all four answers present at the time I wrote this which suggests you aren't clearly indicating what the issue is. – msw Jul 24 '11 at 11:36

4 Answers4

5

I find that in many cases, using a configuration file is really worth the (minor) hassle. The builtin ConfigParser module is very handy, especially the fact that it's really easy to parse multiple files and let the module merge them together, with values in files parsed later overriding values from files parsed earlier. This allows for easy use of a global file (e.g. /etc/yoursoftware/main.ini) and a per-user file (~/.yoursoftware/main.ini).

Each of your projects would then open the config file and read values from it.

Here's a small code example:

basefile.ini:

[sect1]
value1=1
value2=2

overridingfile.ini:

[sect1]
value1=3

configread.py:

#!/usr/bin/env python

from ConfigParser import ConfigParser

config = ConfigParser()
config.read(["basefile.ini", "overridingfile.ini"])

print config.get("sect1", "value1")
print config.get("sect1", "value2")

Running this would print out:

3
2
Erik Forsberg
  • 4,819
  • 3
  • 27
  • 31
  • 1
    ConfigParser is very useful, and I'm actually already using it. However, what I'm trying to figure out how to *share* these constants *between projects*. – Jonathan Livni Jul 24 '11 at 09:59
3

Create a Python package and import it in the various projects...

Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
3

Why don't you just have a file named constants.py and just have CONSTANT = value

Pwnna
  • 9,178
  • 21
  • 65
  • 91
  • See the update for clarification. I don't see how a simple constants.py file would work. I would have to maintain it in each and every project... – Jonathan Livni Jul 24 '11 at 11:04
  • If they are constants that suggests they are constant. How would `import my.constant.user` work in your scenario if `my/constant/user.py` were not also deployed along with the rest of the code? Once you answer that, `my.constant.initializer.py` or `my/constants.py` should deploy in the same manner. – msw Jul 24 '11 at 11:42
  • Make a package and install it to python's library directory (i don't remember the specific directory) – Pwnna Jul 25 '11 at 09:26
2

Why is a database overkill? You're describing sharing data across different projects located on different physical systems with different paths to each project's directory. Oh, and sometimes the projects just aren't there. I can't imagine a better means of communicating the data. It only has to be a single table, that's hardly overkill if it provides the consistent access you need across platforms, computers, and even networks.

Jonathanb
  • 1,224
  • 1
  • 11
  • 16
  • A DB will solve this of course. I can use an attribute-value model which would loosen coupling. However, in this question I'm trying to look for the next-best alternative. +1 as this will of course solve the problem. – Jonathan Livni Jul 24 '11 at 11:40