简体   繁体   English

configparser.NoSectionError:No section:'XXX'

[英]configparser.NoSectionError:No section:'XXX'

I want to configure a config.我想配置一个配置。 ini and read it. ini 并阅读它。 When I run the code in pycharm, it's okay and it returns the result normally.当我在pycharm中运行代码时,它可以正常返回结果。 But when I use pyinstaller to package .PY into an .EXE file, it will report an error:但是当我使用pyinstaller将.PY打包成.EXE文件时,会报错:

Congparser.NosectionError: No section:'config'

If there are any suggestions, I would be very grateful.如果有任何建议,我将不胜感激。 By the way, the.顺便说一下,。 EXE file and. EXE 文件和。 INI file are in the same folder, I use Python3.7 on Windows10 INI文件在同一个文件夹中,我在Windows10上使用Python3.7

I'm a Python rookie.我是 Python 菜鸟。 I don't know how to solve the error in cmd.我不知道如何解决cmd中的错误。 I try to output the path in pycharm.我尝试在pycharm中输出路径。 The result is very normal.结果很正常。

# 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)

Results in pycharm: 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']

Results in CMD: 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

Thanks for Rolf of Saxony!感谢萨克森的罗尔夫! I solved this problem by change我通过改变解决了这个问题

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

to

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

it turns out that the resluts in pycharm are different from those in EXE, hopefully this will help more people原来pycharm里的resluts和EXE里的不一样,希望能帮到更多人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM