如何在使用带有env的Popen()时让“adb设备”工作

时间:2013-02-10 09:36:20

标签: python subprocess adb popen

原始代码为here

import subprocess as sp

cmd = ["adb","push","file","/mnt/sdcard/file"]
mysp = sp.popen(cmd, env={'ADB_TRACE':'adb'}, stdout=sp.PIPE, stderr=sp.PIPE)
stdout,stderr = mysp.communicate()

if mysp.returncode != 0:
    print stderr
else:
    print stdout

没有env={'ADB_TRACE':'adb'},它可以正常工作。

使用env变量执行任何关于adb的命令,我收到错误:

ADB server didn't ACK
* failed to start daemon *
error: cannot connect to daemon

杀死adb服务器后似乎无法正常工作

整个输出为here

OS:WIN7

1 个答案:

答案 0 :(得分:1)

我怀疑adb还需要其他环境变量(例如$HOME)。 您应克隆现有环境并向其添加ADB_TRACE

import os
new_env = os.environ.copy()
new_env['ADB_TRACE'] = 'adb'

# sp.popen()

来自文档:

If env is not None, it must be a mapping that defines the environment variables
for the new process; these are used instead of inheriting the current process’
environment, which is the default behavior.

编辑:

看起来,这不是关于环境本身。 相反,如果设置了ADB_TRACE,则adb服务器会中断。 尝试在没有ADB_TRACE的环境中预先启动服务器。