解决 Pyinstaller 6.0.0版本后,打包oneDir时非exe文件被默认归纳到_internal 文件夹问题
现象描述:
自 Pyinstaller>=6.0.0 版本后,在打包 one dir(-D 目录模式)时,除可执行文件外,其余文件都将被转移到 _internal 文件夹下

官方原文:
Restructure onedir mode builds so that everything except the executable (and .pkg if
you’re using external PYZ archive mode) are hidden inside a sub-directory. This
sub-directory’s name defaults to _internal but may be configured with a new
--contents-directory option. Onefile applications and macOS .app bundles are unaffected.
产生的问题:
由此产生的问题就是,旧项目中凡是直接使用相对路径调用的文件,在使用 Pyinstaller>=6.0.0 版本打包后,运行可执行文件时,会找不到这些文件。
解决方案:
-
方案1:
将相对路径替换为生成路径
官方原文
All of onedir build’s contents except for the executable are now moved into a sub-directory (called _internal by default). sys._MEIPASS is adjusted to point to this _internal directory. The breaking implications for this are: 1.Assumptions that os.path.dirname(sys.executable) == sys._MEIPASS will break. Code locating application resources using os.path.dirname(sys.executable) should be adjusted to use __file__ or sys._MEIPASS and any code locating the original executable using sys._MEIPASS should use sys.executable directly. 2.Any custom post processing steps (either in the .spec file or externally) which modify the bundle will likely need adjusting to accommodate the new directory.修改程序通过模块的
__file__属性拼装一个路径来引用它们""" @ File : module.py @ Author : yqbao """ print(__file__) # 输出:F:\Projects\test\module.py -
方案2:
保留旧版本的one dir 布局
官方原文
Allow users to re-enable the old onedir layout (without contents directory) by settings the --contents-directory option (or the equivalent contents_directory argument to EXE in the .spec file) to '.'.使用
--contents-directory参数,打包时设置--contents-directory .来使其启用旧版本的one dir 布局注意此参数生效需要
pyinstaller>=6.1.0 -
方案3:
降低
pyinstaller版本,pyinstaller < 6.0.0
本文来自博客园作者:星尘的博客,转载请注明出处:https://www.cnblogs.com/yqbaowo/p/17863429.html

浙公网安备 33010602011771号