I am trying to modify the default .spec file created by Pyinstaller to include hidden imports and datas however everytime I run pyinstaller and specify the spec file like pyinstaller source.py spource.spec my source.spec gets rest to its default state i.e.
a = Analysis(['source.py'],
pathex=['C:\\PATHTOSOURCE'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
from PyInstaller.utils.hooks import collect_data_files, collect_submodules
Mydatas = collect_data_files("skimage.io._plugins")
Myhiddenimports = collect_submodules('skimage.io._plugins')
a = Analysis(['source.py'],
pathex=['C:\\PATHTOSOURCE'],
binaries=[],
datas=Mydatas,
hiddenimports=Myhiddenimports,
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
But again this gets reset to the above default analysis class Thanks so much
For anyone still struggling with this issue. This was happening to me as well, but it was due to human error.
When you first build your project through PyInstaller you likely did something like this:
pyinstaller mymodule.py
This, among other things, will generate the .spec
file. If you edit this .spec
file and want to build your script using this spec file, make sure to point PyInstaller at that file.
pyinstaller mymodule.spec
I also recommend making a backup of your .spec
file as you make changes. That way, if you accidentally re-use the mymodule.py
path, you can quickly restore the changes you made to the spec.
The official way is using makespec.py
script (come with PyInstaller
module) to create permanent spec file first (This example is my Windows system, you need modify the python path of your system), pass the options which same as PyInstaller options (If the makespec.py
path changed in future, try to find the file start from PyInstaller
module path):
"c:\Program Files\Python38\python.exe" "c:\Program Files\Python38\Lib\site-packages\PyInstaller\utils\cliutils\makespec.py" -F --noconsole --icon=example_small.ico example.py
Then edit the generated file example.spec
as you wish, then do this to generate exe file as PyInstaller
do:
"c:\Program Files\Python38\python.exe" -m PyInstaller example.spec
In future, you can simply rerun second command above to generate new exe after modified your python code. But sometime(e.g. import new library) you need to edit the example.spec
file, do it in either of two ways:
example.spec
file, then rerun the second commandexample.spec
file will get replaced by
first command. You should backup example.spec
first if you edited example.spec
manually before, so you can edit example.spec
by referring backup file after regenerate example.spec
.More info:
pyi-makespec
command if it already in environment path instead of makespec.py
file I mentioned above. As stated:
pyi-makespec
is used to create a spec file. See Using Spec Files.
If you do not perform a complete installation (installing via
pip
or executingsetup.py
), these commands will not be installed as commands. However, you can still execute all the functions documented below by running Python scripts found in the distribution folder. The equivalent of thepyinstaller
command ispyinstaller-folder/pyinstaller.py
. The other commands are found inpyinstaller-folder/cliutils/
with meaningful names (makespec.py
, etc.)
Amr
I too am struggling with the spec file but think I know the answer to your issue. pyinstaller source.py creates a default spec file if one doesn't already exist. pyinstaller source.py will use source.spec if one is currently in the same directory where you have executed pyinstaller source.py command. if a spec file is available you can execute pyinstaller source.spec and your build will use values from that spec file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With