Search code examples
pythonexeinno-setupdistributionsoftware-distribution

"Failed to execute script" error in executable file created by innosetup


I created a simple script like the following.

This simply creates a text.txt file in the same location as the executable.

test.py

import os,sys
test_str = "test"

exeFileDir = os.path.dirname(os.path.abspath(sys.argv[0]))
path_w =  os.path.join(exeFileDir, 'test.txt')
with open(path_w, mode='w') as f:
    f.write(test_str)

And I created an exe file using Pyinstaller.

pyinstaller --noconsole test.py

At this point, running the exe file ran the code correctly and created a test.txt file.

Finally, I created a setup file using innosetup.

However, when I ran exe after running innosetup, I got the following error.

enter image description here

It seems that innosetup caused such an error. What caused it?

My innosetup configuration file is as follows.

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "testApp"
#define MyAppVersion "1.00"
#define MyAppPublisher "My Company, Inc."
#define MyAppURL "http://www.example.com/"
#define MyAppExeName "test.exe"

[Setup]
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{3D8E37F3-FB32-4AF9-8C64-58C37D542248}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\{#MyAppName}
DisableProgramGroupPage=yes
; Uncomment the following line to run in non administrative install mode (install for current user only.)
;PrivilegesRequired=lowest
OutputDir=C:\Users\taichi\Desktop
OutputBaseFilename=mysetup
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

[Files]
Source: "C:\Users\taichi\Desktop\dist\test\test.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Users\taichi\Desktop\dist\test\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent

Solution

  • "It seems that innosetup caused such an error. What caused it?"

    No, the error comes from the python script !! pyinstaller : Failed to execute script

    __ file __ is the name of the current file, which may be different from the main script if you are inside a module or if you start a script using execfile()

    Difference between --file-- and sys.argv[0]

    test.exe vs test.py

    os.path.abspath(sys.argv[0]) may fail if not also test.py is in the same folder as test.exe

    exeFileDir = os.path.dirname(os.path.abspath(sys.argv[0]))
    

    To get the dirname of the absolute path, use

    exeFileDir = os.path.dirname(os.path.abspath(__file__))
    

    and now path_w should be correct

    path_w =  os.path.join(exeFileDir, 'test.txt')