0

我正在尝试在 UNIX 上运行 .mpkg 安装程序,当我运行此代码时:

p = subprocess.Popen(['/Path/to/File.mpkg'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
return p

输出是这样的:

<subprocess.Popen object at 0x11384xxx>

我的第一个问题是 - 有没有更简单的方法在 UNIX 上运行 mpkg 安装程序?其次 - 我似乎无法弄清楚如何使用 subprocess.Popen 对象对我有利。

4

1 回答 1

0

Sadly I’m not sure what mpkg is, but there are two options.

  • Either it is a self-running package, perhaps a shell script, akin to the .run format sometimes used for Unix software. In this case, your invocation of Popen is correct, as long as you have the execute permission on File.mpkg (check with ls -l /Path/to/File.mpkg). The installer should be running fine.
  • Or, it is intended to be processed by a system tool, like .deb packages are handled with the dpkg program. In this case, you need something like this:

    p = subprocess.Popen(['/usr/bin/dpkg', '-i', '/Path/to/File.deb'], ...)
    

    or, alternatively:

    p = subprocess.Popen('dpkg -i /Path/to/File.deb', ..., shell=True)
    

Now, what you do with this Popen object depends on what you want to achieve. If you wish to get the output of the process, you need to call Popen.communicate, like this:

p = subprocess.Popen(
    ['/Path/to/File.mpkg'],
    stdout=subprocess.PIPE, stderr=subprocess.PIPE,
    stdin=subprocess.PIPE)
(out, err) = p.communicate()

Now out contains the standard output and err the standard error.

If you just want to invoke the command and wait until it completes, you can also use the subprocess.call shortcut.

于 2012-06-22T14:10:03.073 回答