I have the following directory structure:
my_program/ foo.py __init__.py # empty conf/ config.cfg __init__.py
In foo.py I have this:
import sys #sys.path.append('conf/') import ConfigParser config = ConfigParser.ConfigParser() config.read( 'conf/config.cfg' )
In conf/__init__.py
I have
__all__ = ["config.cfg"]
I get this error in foo.py
that I can fix by giving the full path but not when I just put conf/config.cfg
but I want the relative path to work:
ConfigParser.NoSectionError
which actually means that the file can't be loaded (so it can't read the section).
I've tried commenting/un-commenting sys.path.append('conf/')
in foo.py
but it doesn't do anything.
Any ideas?
Source code: Lib/configparser.py. This module provides the ConfigParser class which implements a basic configuration language which provides a structure similar to what's found in Microsoft Windows INI files.
The configparser module from Python's standard library defines functionality for reading and writing configuration files as used by Microsoft Windows OS. Such files usually have . INI extension. The INI file consists of sections, each led by a [section] header.
configparser comes from Python 3 and as such it works well with Unicode. The library is generally cleaned up in terms of internal data storage and reading/writing files.
Just use a StringIO object and the configparser's write method. It looks like the only method for "printing" the contents of a config object is ConfigParser. write which takes a file-like object.
Paths are relative to the current working directory, which is usually the directory from which you run your program (but the current directory can be changed by your program [or a module] and it is in general not the directory of your program file).
A solution consists in automatically calculating the path to your file, through the __file__
variable that the Python interpreter creates for you in foo.py
:
import os config.read(os.path.join(os.path.dirname(__file__), 'conf', 'config.cfg'))
Explanation: The __file__
variable of each program (module) contains its path (possibly relative to the current directory when it was loaded, I guess—I could not find anything conclusive in the Python documentation—, which happens for instance when foo.py
is imported from its own directory).
This way, the import works correctly whatever the current working directory, and wherever you put your package.
PS: side note: __all__ = ["config.cfg"]
is not what you want: it tells Python what symbols (variables, functions) to import when you do from conf import *
. It should be deleted.
PPS: if the code changes the current working directory between the time the configuration-reading module is loaded and the time you read the configuration file, then you want to first store the absolute path of your configuration file (with os.path.abspath()
) before changing the current directory, so that the configuration is found even after the current directory change.
you can use os
package in python for importing an absolute or relative path to the configuration file. Here is a working example with the relative path (we suppose that config.ini
in the folder name configuration
that is in the subdirectory of your python script folder):
import configparser import os path_current_directory = os.path.dirname(__file__) path_config_file = os.path.join(path_current_directory, 'configuration', config.ini) config = configparser.ConfigParser() config.read(path_config_file)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With