Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating .Spec file for Pyinstaller gets reset

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)

what I am trying to accomplish is

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

like image 214
Amr Abdu Avatar asked Mar 03 '18 11:03

Amr Abdu


3 Answers

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.

like image 65
Brandon Barney Avatar answered Sep 28 '22 07:09

Brandon Barney


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:

  • Edit example.spec file, then rerun the second command
  • Edit first command options, then rerun both commands. And of course, the example.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:

  • https://pyinstaller.readthedocs.io/en/stable/spec-files.html
  • https://pyinstaller.readthedocs.io/en/stable/man/pyi-makespec.html
  • You can use 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 executing setup.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 the pyinstaller command is pyinstaller-folder/pyinstaller.py. The other commands are found in pyinstaller-folder/cliutils/ with meaningful names (makespec.py, etc.)

like image 29
林果皞 Avatar answered Sep 28 '22 05:09

林果皞


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.

like image 45
T Campion Avatar answered Sep 28 '22 05:09

T Campion