我得到一个错误,当我用pyinstaller编译时,它在我的pc上工作,而不是在另一台pc上。
我得到了错误
Traceback (most recent call last):
File "app.py", line 30, in <module>
password = parser.get('settings', 'password')
File "configparser.py", line 781, in get
File "configparser.py", line 1152, in _unify_values
configparser.NoSectionError: No section: 'settings'
[9464] Failed to execute script 'app' due to unhandled exception!
以下是我的代码
parser = ConfigParser()
parser.read('C:\\Users\\abc\Desktop\\Maker\\settings.ini')
我遵循了一些解决方案,但仍然没有运气来解决这个问题,有人能帮忙吗?
我也尝试了这个解决方案,但没有成功。
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
parser = ConfigParser()
settings_file = resource_path('settings.ini')
发布于 2021-08-25 01:01:50
您必须确保将ini文件与应用程序捆绑在一起。如果您使用UPX将所有文件压缩为一个可执行文件,这会有点困难。我从来没有看到这样做的意义,因为它看起来像是一种虚假的节省。每次运行EXE时,它会将所有文件解压缩到一个临时文件夹中。然后,您的应用程序现在将消耗所有未压缩空间和所有压缩空间。布利亚。
我建议您使用Spec file来指定要包含在应用程序中的所有附加文件。
出于调试目的,您可以将此代码放入程序中,以确保ini文件位于您认为的位置(一旦验证了该文件,即可将其删除或注释掉)
print(os.listdir(<path>))
或
print(os.listdir(os.getcwd()))
其中getcwd代表‘获取当前工作目录’,它应该是你的EXE所在的目录。
如果你不想弄乱一个规范文件,你可以将--add-data
参数传递给pyinstaller并传递你的ini文件。
https://stackoverflow.com/questions/68915346
复制相似问题