如何在将 Popen() 与 env 一起使用时让"adb device"工作



原始代码在这里

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

它在没有CCD_ 1的情况下工作良好。

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

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

杀死adb服务器后似乎不起作用

全部输出在这里

操作系统:win7

我怀疑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的环境中预先启动服务器。

最新更新