Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put a configuration file in Python?

In development mode, I have the following directory tree :

| my_project/ | setup.py | my_project/     | __init__.py     | main.py     | conf/         | myproject.conf 

I use ConfigParser to parse the myproject.conf file.

In my code, it's easy to load the file with a good path : my_project/conf/myproject.conf

The problem is : When I install my project using the setup.py, the configuration file are situated (thanks to setup.py) in the /etc/my_project/myproject.conf and my application in the /usr/lib/python<version>/site-packages/my_project/.

How can I refer my my_project/conf/myproject.conf file in my project in "production" mode, and refer to the local file (my_project/conf/myproject.conf) in "devel" mode.

In addition, I would like to be portable if possible (Work on windows for example).

What's the good practice to do that ?

like image 645
Sandro Munda Avatar asked Sep 27 '11 10:09

Sandro Munda


People also ask

Where do I put config files?

The vast majority of Linux config files can be found in the /etc/ directory or a sub-directory. Most of the time these configuration files will be edited through the command line, so get comfortable with applications like Nano or Vi.

How do you write to config file in Python?

Python Configuration File The simplest way to write configuration files is to simply write a separate file that contains Python code. You might want to call it something like databaseconfig.py . Then you could add the line *config.py to your . gitignore file to avoid uploading it accidentally.

What is config () in Python?

A Python configuration file is a pure Python file that populates a configuration object. This configuration object is a Config instance.


Video Answer


1 Answers

Have you seen how configuration files work? Read up on "rc" files, as they're sometimes called. "bashrc", "vimrc", etc.

There's usually a multi-step search for the configuration file.

  1. Local directory. ./myproject.conf.

  2. User's home directory (~user/myproject.conf)

  3. A standard system-wide directory (/etc/myproject/myproject.conf)

  4. A place named by an environment variable (MYPROJECT_CONF)

The Python installation would be the last place to look.

config= None for loc in os.curdir, os.path.expanduser("~"), "/etc/myproject", os.environ.get("MYPROJECT_CONF"):     try:          with open(os.path.join(loc,"myproject.conf")) as source:             config.readfp( source )     except IOError:         pass 
like image 187
S.Lott Avatar answered Oct 05 '22 05:10

S.Lott