2013-02-10 54 views
0

用popen()时得到 “ADB设备” 工作的原代码是here如何与ENV

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'}

EXEC任何命令和环境变量有关adb,我得到了一个错误:

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

似乎不工作后杀死ADB服务器

整个输出here

OS :win7

+0

如果您通过命令行设置了'ADB_TRACE'并运行'adb',它会工作吗?它的工作原理是 – Vlad 2013-02-10 11:43:56

回答

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. 

编辑:

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

+0

。 thx很多:D – tastypear 2013-02-10 13:46:00

+0

随时接受答案:-) – 2013-02-10 14:32:20