[英]configparser.NoSectionError: No section: 'database'
[英]configparser.NoSectionError:No section:'XXX'
我想配置一個配置。 ini 並閱讀它。 當我在pycharm中運行代碼時,它可以正常返回結果。 但是當我使用pyinstaller將.PY打包成.EXE文件時,會報錯:
Congparser.NosectionError: No section:'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里的resluts和EXE里的不一樣,希望能幫到更多人
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.