2013-02-10 78 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