python - 配置解析器.NoSectionError :No section :'XXX'

标签 python pycharm pyinstaller configparser

我想配置一个配置。 ini 并读取它。当我在pycharm中运行代码时,没有问题,并且正常返回结果。但是当我使用pyinstaller将.PY打包成.EXE文件时,会报错:

Congparser.NosectionError:没有部分:'config'

如果有任何建议,我将不胜感激。 顺便说一下,. EXE 文件和 . INI 文件位于同一文件夹中, 我在Windows10上使用Python3.7

我是一个Python菜鸟。我不知道如何解决cmd中的错误。我尝试在pycharm中输出路径。结果很正常。

# coding = gbk

import  configparser

import os

curpath = os.path.dirname(os.path.realpath(__file__))
cfgpath = os.path.join(curpath, "config.ini")
print(cfgpath)
print(os.path.realpath(__file__))


conf = configparser.ConfigParser()


conf.read(cfgpath)


items = conf.items('config')
l2 = [items[0][1],items[1][1],items[2][1],items[3][1]]
print(items)
print(l2)

pycharm中的结果:

E:\untitled\venv\Custom_formula\config.ini
E:\untitled\venv\Custom_formula\config_data.py
[('server', '127.0.0.1'), ('user', 'sa'), ('pwd', '123456'), ('db', 'test')]
['127.0.0.1', 'sa', '123456', 'test']

CMD 结果:

Traceback (most recent call last):
  File "config_data.py", line 25, in <module>
  File "configparser.py", line 848, in items
configparser.NoSectionError: No section: 'config'
[9080] Failed to execute script config_data

最佳答案

感谢萨克森的罗尔夫!我通过改变解决了这个问题

curpath = os.path.dirname(os.path.realpath(__file__))

curpath = os.path.dirname(os.path.realpath(sys.argv[0]))

原来pycharm中的结果和EXE中的结果不一样, 希望这可以帮助更多的人

关于python - 配置解析器.NoSectionError :No section :'XXX' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57345183/

相关文章:

python - 使用 pyinstaller 在 pygame 脚本中包含声音文件

python - 仅查找列表中的唯一坐标

python - 用于文本的简单过滤器 Python 脚本

python - 如何在开发过程中构建 Python CFFI 模块?

python - PyCharm 模块导入停止正常工作 (OS X)

pycharm - 如何在 PyCharm 中导入?

python - 关闭 'Python version 3.5 does not support variable annotations' 错误信息

python - 如何将队列字典传递给 multiprocessing.Process

python - pyinstaller生成的exe文件在其他计算机上有延迟

python-3.x - 属性错误 : Module 'PyQt5' has no attribute '__version__'