how to get “adb device” to work when using Popen()

2019-08-30 11:33发布

the original code is 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

it works fine without env={'ADB_TRACE':'adb'}.

exec any command about adb with the env variable, i got an error:

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

it seems not to work after kill the adb server

the whole output is here

OS:win7

1条回答
做自己的国王
2楼-- · 2019-08-30 11:53

I suspect adb also needs other environment variables (like $HOME). You should clone your existing environment and add ADB_TRACE to it.

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

# sp.popen()

From the docs:

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.

Edit:

It seems, it's not about the envoronment itself. Rather the adb server is broken if ADB_TRACE is set. Try to start the server beforehand in an environment without ADB_TRACE.

查看更多
登录 后发表回答