Python 无法使用子目录中的 ConfigParser 加载相关配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13800515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't load relative config file using ConfigParser from sub-directory
提问by ale
I have the following directory structure:
我有以下目录结构:
my_program/
foo.py
__init__.py # empty
conf/
config.cfg
__init__.py
In foo.py I have this:
在 foo.py 我有这个:
import sys
#sys.path.append('conf/')
import ConfigParser
config = ConfigParser.ConfigParser()
config.read( 'conf/config.cfg' )
In conf/__init__.pyI have
在conf/__init__.py我有
__all__ = ["config.cfg"]
I get this error in foo.pythat I can fix by giving the full path but not when I just put conf/config.cfgbut I want the relative path to work:
我收到此错误,foo.py因为我可以通过提供完整路径来修复,但不能在我刚刚放置时修复,conf/config.cfg但我希望相对路径起作用:
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.pybut it doesn't do anything.
我试过评论/取消评论sys.path.append('conf/'),foo.py但它没有做任何事情。
Any ideas? Many thanks.
有任何想法吗?非常感谢。
采纳答案by Eric O Lebigot
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 notthe 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:
解决方案包括通过__file__Python 解释器在 中为您创建的变量自动计算文件的路径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.pyis imported from its own directory).
说明:__file__每个程序(模块)的变量都包含它的路径(可能相对于加载时的当前目录,我猜——我在 Python 文档中找不到任何确定性的东西——,例如foo.py从它自己的导入时会发生这种情况目录)。
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.
PS:旁注:__all__ = ["config.cfg"]不是你想要的:它告诉 Python 在你执行from conf import *. 它应该被删除。
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 absolutepath 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.
PPS:如果代码在加载配置读取模块和读取配置文件之间更改了当前工作目录,那么在更改当前目录之前,您要先存储配置文件的绝对路径(with os.path.abspath()) ,以便即使在当前目录更改后也能找到配置。
回答by Shiva
Just load the path from a Variable and why so complex?
只需从变量加载路径,为什么这么复杂?
from configparser import ConfigParser
prsr=ConfigParser()
configfile="D:\Dummy\DummySubDir\Dummy.ini"
prsr.read(configfile)
print(prsr.sections())
回答by Dr. Arslan
you can use ospackage 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.iniin the folder name configurationthat is in the subdirectory of your python script folder):
您可以使用ospython 中的包来导入配置文件的绝对或相对路径。这是一个带有相对路径的工作示例(我们假设config.ini在configurationpython 脚本文件夹的子目录中的文件夹名称中):
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)

