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
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:
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')],
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.
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
.