pyinstall打包资源文件

Posted chusiyong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyinstall打包资源文件相关的知识,希望对你有一定的参考价值。

相关代码

main.py

import sys
import os

#生成资源文件目录访问路径
#说明: pyinstaller工具打包的可执行文件,运行时sys。frozen会被设置成True
#      因此可以通过sys.frozen的值区分是开发环境还是打包后的生成环境
#
#      打包后的生产环境,资源文件都放在sys._MEIPASS目录下
#      修改main.spec中的datas,
#      如datas=[(‘res‘, ‘res‘)],意思是当前目录下的res目录加入目标exe中,在运行时放在零时文件的根目录下,名称为res

def resource_path(relative_path):
    if getattr(sys, frozen, False):
        base_path = sys._MEIPASS
    else:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)

filename = resource_path(os.path.join("res","a.txt"))
print(filename)

with open(filename) as f:
    lines = f.readlines()
    print(lines)
    f.close()

 

 res/a.txt

1 2 3
4 5 6

 

 打包流程

  1.  生成main.spec文件:pyi-makespec -F test.py
  2. 修改mian.spec文件,如下
  3. pyinstaller -F main.spec
# -*- mode: python -*-

block_cipher = None


a = Analysis([main.py],
             pathex=[C:\Users\xxx\PycharmProjects\Test],
             binaries=[],
             datas=[(res, res)], //重点
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name=main,
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

 

 

pyinstall遇到的问题

描述:

Traceback (most recent call last):
  File "C:Pythonlib
unpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:Pythonlib
unpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:Users10223394PycharmProjectsTestvenvScriptspyinstaller.exe\__main__.py", line 9, in <module>
  File "c:users10223394pycharmprojects	estvenvlibsite-packagesPyInstaller\__main__.py", line 111, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "c:users10223394pycharmprojects	estvenvlibsite-packagesPyInstaller\__main__.py", line 63, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "c:users10223394pycharmprojects	estvenvlibsite-packagesPyInstalleruildinguild_main.py", line 838, in main
    build(specfile, kw.get(distpath), kw.get(workpath), kw.get(clean_build))
  File "c:users10223394pycharmprojects	estvenvlibsite-packagesPyInstalleruildinguild_main.py", line 784, in build
    exec(text, spec_namespace)
  File "<string>", line 32, in <module>
  File "c:users10223394pycharmprojects	estvenvlibsite-packagesPyInstalleruildingapi.py", line 424, in __init__
    strip_binaries=self.strip, upx_binaries=self.upx,
  File "c:users10223394pycharmprojects	estvenvlibsite-packagesPyInstalleruildingapi.py", line 196, in __init__
    self.__postinit__()
  File "c:users10223394pycharmprojects	estvenvlibsite-packagesPyInstalleruildingdatastruct.py", line 158, in __postinit__
    self.assemble()
  File "c:users10223394pycharmprojects	estvenvlibsite-packagesPyInstalleruildingapi.py", line 273, in assemble
    pylib_name = os.path.basename(bindepend.get_python_library_path())
  File "C:Pythonlib
tpath.py", line 214, in basename
    return split(p)[1]
  File "C:Pythonlib
tpath.py", line 183, in split
    p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not NoneType

 

解决办法:

  这是个已知的故障,官方已经修改,但是没有正式的版本支持。下载https://github.com/Loran425/pyinstaller/tree/14b6e65642e4b07a4358bab278019a48dedf7460中PyInstaller里的bindepend.py文件,然后替换掉本地安装包里的bindepend.py文件即可。

以上是关于pyinstall打包资源文件的主要内容,如果未能解决你的问题,请参考以下文章

Pyinstaller如何将资源文件一起打包至exe中

Pyinstaller如何将资源文件一起打包至exe中

解决pyinstaller打包过程中外部资源无法加载的问题 ...

Pyinstaller如何打包多个资源文件(超详细教程)

pyinstaller打包,exe图片资源无法显示问题

Python常用模块:pyinstaller打包生成exe