7

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

当然,如果 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;

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

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

有人已经成功使用带有 innosetup 的 python 脚本了吗?

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

现在我使用 CXfreeeeze 制作脚本的 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()

编辑 *

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

当我使用命令行 ALLUSERS=1 为所有用户安装 python 时,现有用户(或管理员)可以运行 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

在代码中:

  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)); 
4

1 回答 1

5

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

存在两种不同的可能性:

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