Pyinstaller on a setuptools package

前端 未结 5 1768
既然无缘
既然无缘 2021-01-11 09:33

I\'m attempting to run PyInstaller on a CLI app I am building in Python using the Click library. I\'m having trouble building the project using PyInstaller. PyInstaller has

5条回答
  •  甜味超标
    2021-01-11 10:25

    After much searching, this error is generally due to attempting to access your project's package's metadata (i.e. version being the primary one).

    Package metadata is typically accessed using pkg_resources or the older distutil, either explicitly, or often hidden in other packages (usually attempting to access the package version). Starting with Python v3.8 it will also be available in the stdlib within importlib.metadata.

    If this is the case, you likely need to include some or all of the files in a mypackage.egg-info folder, especially the file PKG_INFO, but it may require them all.

    There are multiple ways to do this, here are several I like:


    1. If you are using a script.spec file, you can update the datas= line to include this info, per Charles answer:

    a = Analysis(['myscript.py'],
                 pathex=['C:\\path\\to\\mypackage'],
                 binaries=[],
                 datas=[('mypackage.egg-info/*','mypackage.egg-info')],
    

    2. Create a custom hook file, put it in a directory, and add the directory as a custom hooks directory at the command line.

    Create a hook-mypackage.py hook file, with the following very simple, and pretty elegant, lines:

    from PyInstaller.utils.hooks import copy_metadata
    
    datas = copy_metadata('md2mat')
    

    I put this into a new hooks folder in my root package/repo folder, then added the following to my pyinstaller command:

    pyinstaller -F -y --additional-hooks-dir=hooks myscript.py
    

    It works pretty well, and assuming the copy_metadata function is well maintained as we change over from older metadata packages to the new importlib.metadata, it should work well through future Python updates.


    3. Add additional data files directly at the command-line

    This might be my favorite, if I could get it working...

    pyinstaller --add-data  myscript.py
    

    This option --add-data shows up in the help output (pyinstaller --help), and indicates the format of the argument should be SRC;DEST for Windows, so I think it must match the datas= format from the other methods, but I couldn't get it to work.

    The closest I think I got to the right format were the following:

    pyinstaller -F -y --add-data "mypackage.egg-info/*;mypackage.egg-info"
    pyinstaller -F -y --add-data="mypackage.egg-info/*;mypackage.egg-info"
    

    These would compile, but the resulting exe would just run with no output.

    The --add-data option is missing from the PyInstaller Documentation, but shows up when running pyinstaller --help-commands.

提交回复
热议问题