使用pyinstaller和configparser编译时出错



当我用pyinstaller编译时,它在我的pc上工作而不是在其他pc上。

error I get

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\abcDesktop\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')

您必须确保将ini文件与应用程序捆绑在一起。如果您使用UPX将所有文件压缩到一个可执行文件中,这就有点困难了。我从来没有看到这有什么意义,因为这似乎是一种虚假的储蓄。每次运行EXE时,它都会将所有文件解压缩到临时文件夹中。然后,应用程序现在消耗所有未压缩空间和所有压缩空间。Bleah .

我建议您使用Spec文件来指定所有附加的要包含在应用程序中的文件。

出于调试的目的,你可以把这个放在你的程序中,以确保ini文件在你认为的地方(你可以删除它,或者在你确认文件在那里后把它注释掉)

print(os.listdir(<path>))

print(os.listdir(os.getcwd()))

其中getcwd代表"获取当前工作目录",应该是您的EXE所在的目录。

如果你不想弄乱一个规范文件,你可以将--add-data参数传递给pyinstaller,并传递你的ini文件。

最新更新