如何使用innoSetup执行python脚本

use*_*873 6 python installation inno-setup python-3.3

我尝试在InnoSetup生成的设置期间运行python脚本,但没有任何效果.代码部分结果代码中的运行部分或执行代码都不同,这取决于我调用它的方式.

当然,如果它不存在,我会在安装过程中安装Python.这是测试代码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;
Run Code Online (Sandbox Code Playgroud)

我尝试在运行部分和代码中直接使用python.exe:Exec但没办法.

当然,如果我在Windows命令行中键入test.py它可以工作,cmd.exe /cC:\python33\python.exe C:\ app\test.py也可以

有人已经成功使用python脚本与innosetup?

这样做的目的不是分发应用程序的py文件,而是在安装过程中使用python脚本来制作一些东西.

现在我使用CXfreeeze来创建脚本的exe但我更喜欢只保留python脚本而不是exe(用于自动化目的)

信息python测试脚本只是:

import ctypes
def msgbox(message,title):
    ctypes.windll.user32.MessageBoxW(0, message, title, 0)
def debug() : 
    msgbox('test','test test')
debug()
Run Code Online (Sandbox Code Playgroud)

编辑*

正如@Tlama建议我尝试使用OriginalUser在[Run]中使用命令而不是inno设置的管理模式(我使用PrivilegesRequired = admin),但它不起作用.

当我使用命令行为所有用户安装python时ALLUSERS = 1现有用户(或管理员)可以运行python脚本.

我也尝试修改[Run]和CODE:Exec中的WorkingDir但是所有试验都给了我相同的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
Run Code Online (Sandbox Code Playgroud)

在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)); 
Run Code Online (Sandbox Code Playgroud)

Ste*_*nes 5

我怀疑问题在于安装程序启动时路径上不存在 python,并且该路径和其他环境变量(如 PYTHONPATH)未设置在程序运行的范围内。

存在两种不同的可能性:

  1. 使用安装到的绝对路径、要执行的脚本的绝对路径以及在脚本中显式设置诸如PYTHONPATH必要的内容来调用 python - 您可以在测试脚本时使用命令行中的 -E 标志对此进行测试.
  2. 启动一个新的 shell,它将在它的环境中获取新路径等,而不是在当前进程正在运行的当前环境中运行 - 要做到这一点,只需将命令从python somescript.pyto更改 为,(对于 Windows),start python somescript.py应该这样做工作很好。