• 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文件即可。

  • 相关阅读:
    NC20282 棘手的操作(启发式合并)
    CF707D Persistent Bookcase(主席树+bitset)
    CF1473E Minimum Path(分层图+最短路)
    线段树优化建图2模板(暂无正确性保证)
    subprocess
    django中update_or_create()
    django中重复键值违反唯一键约束错误
    tox运行报C901错误解决办法
    gitlab搭建
    git命令
  • 原文地址:https://www.cnblogs.com/chusiyong/p/12052930.html
Copyright © 2020-2023  润新知