7

I try to run a python script during a setup generate by InnoSetup , but nothing works. Neither the Run section or Exec in code section Result code differ depending the way I call it.

Of course I install Python during the setup if it's not already present. Here is the test code Inno

[Setup]
AppName=PyPy_client
AppVersion=0.1
DefaultDirName={pf}\DeployPyPy
UninstallDisplayIcon={app}\test.py
Compression = zip/1
OutputDir=deploy
SetupLogging = yes
UsePreviousGroup=False
DisableProgramGroupPage=yes
PrivilegesRequired = admin

[Files]
Source: "D:\Dev\deploy_python\python-3.3.2.msi"; DestDir: "{app}\deploy"; Flags: ignoreversion
Source: "D:\Dev\deploy_python\test.py"; DestDir: "{app}"; Flags: ignoreversion 

[Run]
Filename: "msiexec"; Parameters: "/i ""{app}\deploy\python-3.3.2.msi"" /qb! ALLUSER=1 ADDLOCAL=ALL"; WorkingDir: "{app}\deploy"; Flags: 32bit; Check: python_is_installed
Filename: "cmd.exe"; Parameters: "/c{code:GetPythonPath}\python.exe {app}\test.py"; WorkingDir: "{app}"; Flags: waituntilterminated

[Code]
function python_is_installed() : Boolean;
var 
  key : string;
begin
  //check registry 
   key := 'software\Python\PythonCore\3.3\InstallPath'
   Result := not RegValueExists(HKEY_LOCAL_MACHINE,Key,'');   
end;

function GetPythonPath(Param : String) : String;
var dir, key : String;
begin
  dir := '';
  key := 'software\Python\PythonCore\3.3\InstallPath'
  RegQueryStringValue(HKEY_LOCAL_MACHINE,key,'',dir);
  Result := dir
end;

procedure DeinitializeSetup();
var
  ResultCode: integer;
begin
  if Exec('cmd.exe', ExpandConstant('/c' +GetPythonPath('')+ '\python.exe {app}\test.py'), '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
    Log(intTostr(Resultcode));
end;

I Try to use directly python.exe in Run section and in code:Exec but no way.

And of course if I type test.py in windows command line it works , and the cmd.exe /cC:\python33\python.exe C:\app\test.py also

Somebody already succes using python script with innosetup ?

The purpose of this is not to distribute py file of an app but using python script during the installation process to make some stuff.

Now I m' using CXfreeeze to make an exe of the scripts but I prefer to keep only the python script and not exe (for automatisation purpose)

for information python test script is just :

import ctypes
def msgbox(message,title):
    ctypes.windll.user32.MessageBoxW(0, message, title, 0)
def debug() : 
    msgbox('test','test test')
debug()

EDIT *

As @Tlama suggest I have tried to use the command in [Run] using OriginalUser instead of the Admin mode setted by inno (I m using the PrivilegesRequired = admin ) but it doesn't Work.

And as I install python for all users with the command line ALLUSERS=1 existing users (or admin) can run python scripts.

I also try to modify the WorkingDir in [Run] and in CODE:Exec but all tentatives give me the same ResultCode "2"

Filename: "cmd.exe"; Parameters: "/c{code:GetPythonPath}\python.exe {app}\test.py"; WorkingDir: "{app}"; Flags: waituntilterminated
Filename: "cmd.exe"; Parameters: "/c{code:GetPythonPath}\python.exe {app}\test.py"; WorkingDir: "{code:GetPythonPath}"; Flags: waituntilterminated
Filename: "python.exe"; Parameters: "{app}\test.py"; WorkingDir: "{code:GetPythonPath}"; Flags: waituntilterminated
Filename: "python.exe"; Parameters: "{app}\test.py"; WorkingDir: "{app}"; Flags: waituntilterminated

in CODE :

  Log('Start pypy 1');    
  Exec('cmd.exe', ExpandConstant('/c' +GetPythonPath('')+ '\python.exe {app}\test.py'), GetPythonPath(''), SW_SHOW, ewWaitUntilTerminated, ResultCode);
  Log(intToStr(Resultcode));  
  Log('Start pypy 2');    
  Exec(GetPythonPath('')+ '\python.exe', ExpandConstant('{app}\test.py'), GetPythonPath(''), SW_SHOW, ewWaitUntilTerminated, ResultCode);
  Log(intToStr(Resultcode));  
  Log('Start pypy 3');    
  Exec('cmd.exe', ExpandConstant('/c' +GetPythonPath('')+ '\python.exe {app}\test.py'),ExpandConstant('{app}'), SW_SHOW, ewWaitUntilTerminated, ResultCode);  
  Log(intToStr(Resultcode)); 
cnaimi
  • 484
  • 6
  • 10
  • Yes I try also : "python.exe" "C:\pyhton33\pyhton.exe test.py" even only th script (python is and .py are linked to python.exe ) "test.py" nothing work. – cnaimi Sep 30 '13 at 06:57
  • Thanks for your help ! I try using originalUser , it has no effect too. I didn't mention but i try several workingdir (pyhton one , app one ..) it has no effet. I think it related to python but this is really strange – cnaimi Sep 30 '13 at 07:16
  • Ok thanks you, I use Inno for years , but this time I am really stuck. – cnaimi Sep 30 '13 at 11:06
  • what about creating a ms batch file (say dopython.bat) and install the python code as a py file itself as part of your install. The Batch file would fire the python file whos final code segment would be instructions to delete itself (and the resulting pyc file), and the batch file when done. Then use inno setup's execution methods to fire the batch script and capture the result. – Jase Oct 04 '13 at 12:28

1 Answers1

5

I suspect that the issue is that python did not exist on the path when the installer started and that path and other environment variables such as PYTHONPATH are not set in the scope that the program is running in.

Two distinct possibilities exist:

  1. Call python with it's absolute path that it was installed to, the absolute path of the script to execute and in your script explicitly set things like PYTHONPATH if necessary - you can test for this by using the -E flag from the command line when testing your script.
  2. Start a new shell, which will get the new path, etc., in it's environment rather than running in the current one that the current process is running in - to do this simply change your command from python somescript.py to, (for windows), start python somescript.py should do the job nicely.
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
  • For this case sounds better the first option... Is there a need to explicitly specify a working directory (I don't know, to some Python's library path) ? How about privileges Python should run ? Installer usually runs with the administrator privileges, would you recommend to run it as the original user ? – TLama Oct 06 '13 at 09:15
  • Required paths is what you test with python -E yourscript.py - admin privileges shouldn't be an issue. – Steve Barnes Oct 06 '13 at 09:51
  • I don't, since I don't have time to play with Python :-) It's upon OP. However, 168 users have seen this question which is quite enough. Bounty is now yours ;-) I'm just wondering that only one answer was posted. Is that so hard to execute Python from *a foreign application* through command line or just nobody is doing that ? – TLama Oct 07 '13 at 07:08
  • 1
    Thank-you @TLama - I have noticed that many people shy away from anything that is basically the command line. – Steve Barnes Oct 07 '13 at 07:20