How to store properties

Peter Heitzer peter.heitzer at rz.uni-regensburg.de
Wed Feb 8 07:11:55 EST 2017


Cecil Westerhof <Cecil at decebal.nl> wrote:
>In Java you (can) use a properties file store configuration. What is
>the best way to do something like that in Python?
>I saw ConfigParser, but have the feeling that it is not really used.
>Would a JSON file be a good idea?

If you only want to read the configuration, just use an ordinary 
file you import. For example config.py contains the lines:
username=myuser
server=myserver
password=secret

In your script:

import config

Now you can referenc all the variables via config.<name>, e.g. config.username

Another method would be a dictionary for your config. You could pickle and
unpickle it.

-- 
Dipl.-Inform(FH) Peter Heitzer, peter.heitzer at rz.uni-regensburg.de



More information about the Python-list mailing list