2

I’m New to Python Coding and just finished my first python scripted I’m trying to publish my programme so that I can install in on another device.

But as soon as I convert it from .py to .exe with pyinstaller and try to run my programme it gives me the error:

fatal error: failed to execute scrip

Code I used in to convert:

pyinstaller -w file_name.py
pyinstaller -F file_name.py
pyinstaller -i "c:\\icon_file path" file_name.py

am I just missing as step or is there something else I can try to resolve this problem? I usually code on Visual studio and when I test run everything worked fine.

My .spec file:

    block_cipher = None


    a = Analysis(['file_name.py'],
                 pathex=['C:\\Users\\MainUser\\Desktop\\Publishing'],
                 binaries=[],
                 datas=[],
                 hiddenimports=[],
                 hookspath=[],
                 runtime_hooks=[],
                 excludes=[],                
                 win_no_prefer_redirects=False,
                 win_private_assemblies=False,
                 cipher=block_cipher,
                 noarchive=False)
    a.binaries = a.binaries + 
                 [('libsha1.dll','/home/iot/lib/libsha1.dll','BINARY')]
    pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
    exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='file_name',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )
```
Tania Rheeder
  • 545
  • 1
  • 6
  • 10
  • Can you share the code you use please for pyinstaller? – Nicholas May 15 '19 at 14:42
  • pyinstaller -w My_File_Name.py ; pyinstaller -F My_File_Name.py ; pyinstaller -i "Path for my Icon_image" My_File_Name.py – Tania Rheeder May 16 '19 at 07:58
  • Does this answer your question? [Windows- Pyinstaller Error "failed to execute script " When App Clicked](https://stackoverflow.com/questions/40716346/windows-pyinstaller-error-failed-to-execute-script-when-app-clicked) – user5994461 May 29 '20 at 21:16

2 Answers2

1

Usually, this is due to a lack of file when packaging.

When you use PyInstaller, you can use it like this:

python -m PyInstaller .\yourFile.py

then, a yourFile.spec file is generated under this folder.

you should edit this file, add all project file into datas,

a = Analysis(['yourFile.py'],
         pathex=['D:\\projectPath\\project'],
         binaries=[],
         datas=[('D:\\projectPath\\project\\*.py', '.'),
                ('D:\\projectPath\\project\\UI\\*.ui', 'UI'),
                ('D:\\projectPath\\project\\other\\*.py', 'other'),
         ],
         ...
    )

It's simulated up here, a project that contains the UI and other folders. It like a tuple, ('full path', 'folder name').

If you have *.dll on Windows or *.so on Linux, you must be write into binaries:

a.binaries = a.binaries + [('libsha1.so','/home/iot/lib/libsha1.so','BINARY')]
jeremyjone
  • 183
  • 12
  • This Might be a silly question, but the path I should enter ”” 'D:\\projectPath\\project' “” is it where my file is publish and I made the .exe file or the one where I originally saved it and all the files I used? – Tania Rheeder May 16 '19 at 09:04
  • I have added my `.spec` file in my main question – Tania Rheeder May 16 '19 at 09:46
  • 1
    @TaniaRheeder Maybe I didn't make myself clear, you need run `python -m PyInstaller` twice, The first time we did that I told, The Second you should run `python -m PyInstaller yourFile.spec`. – jeremyjone May 17 '19 at 14:54
  • @TaniaRheeder ` 'D:\\projectPath\\project'` is your project root path, the *.exe will build in `./dist/startFileName/ *.exe` – jeremyjone May 17 '19 at 14:56
0

I am guessing you only have one script, so if you use:

Pyinstaller --onefile yourScript.py

Replacing yourScript.py with the name of your python file in the CMD/Terminal, you shouldn't have any problems.

If you are missing a binary, this should help. For example pyinstaller was missing the currency converter module, so I found and it, got the zip file and then ran this in CMD:

Pyinstaller --add-binary "C:\Users\myName\Downloads\eurofxref-hist.zip";currency_converter --onefile myScript.py

Where myScript.py is my Python script, and the link is to the folder with the binary zip file.

Nicholas
  • 3,517
  • 13
  • 47
  • 86
  • Should I run this with a setup.py file? It only flesh `command prompt ` for 2 sec and the nothing happens. – Tania Rheeder May 16 '19 at 09:50
  • You shouldn't have to. Create a new folder on your desktop, put your script into it, then open up the CMD in that folder, the paste that code I linked with the script name changed. It should work? – Nicholas May 16 '19 at 09:54
  • So my CMD just gave me a new Error. My data is stored in mySql. Error : No module named mysql.connector but it runs fine when I test it in Visual Studio. And i did pip install It before hand – Tania Rheeder May 16 '19 at 10:17
  • Oh, I see. Sounds like you do not have the supported binary. You need to find the binary zip file of mysql.connector. I have edited my response to help with the code you need to use when you have it – Nicholas May 16 '19 at 10:37
  • Thank Again for all your help. Will keep you updated and let you now if it works. – Tania Rheeder May 16 '19 at 10:38
  • 1
    I just pip Install MySQL-connector and it run, Thank you! – Tania Rheeder May 17 '19 at 10:53