如何在使用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
在没有设置 env={'ADB_TRACE':'adb'}
的情况下,代码运行得很好。
当我用这个环境变量执行任何关于 adb
的命令时,我遇到了一个错误:
ADB server didn't ACK
* failed to start daemon *
error: cannot connect to daemon
在杀掉 adb 服务器后,似乎就不再工作了。
完整的输出可以在 这里 查看。
操作系统:win7
1 个回答
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
的环境中先启动服务器。