现象描述:
自 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.
修改程序通过
sys._MEIPASS
或模块的__file__
属性引用它们 -
方案2:
官方原文
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