问题
I've got running sample python code which is fine in Ubuntu desktop:
import dbus, gobject
from dbus.mainloop.glib import DBusGMainLoop
from dbus.mainloop.glib import threads_init
import subprocess
from subprocess import call
gobject.threads_init()
threads_init()
dbus.mainloop.glib.DBusGMainLoop( set_as_default = True )
p = subprocess.Popen('dbus-launch --sh-syntax', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
call( "export DBUS_SESSION_BUS_ADDRESS" , shell=True )
call( "export DBUS_SESSION_BUS_PID" , shell=True )
bus = dbus.SessionBus()
# get DBUS objects, do other stuff with SESSION_BUS
# in same time we can start more independent processes with this file
# finaly kill the SESSION_BUS process
After success on desktop I moved the code to the server edition which is only with shell. The dbus-launch starts the process, but python dbus.SessionBus() returns error with "/bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed".
Hope there shouldn't be strict dependency between SESSION_BUS and X11 when the process started with "dbus-launch" go up and running with success. The error comes in python.
Best solution will be clean python or linux environment settings, worst but maybe acceptable with some fake or virtual X11 (I was not lucky when I try it)
回答1:
The problem is that you're running the export
calls in separate shells. You need to capture the output of dbus-launch
, parse the values, and use os.environ
to write them to the environment:
p = subprocess.Popen('dbus-launch', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for var in p.stdout:
sp = var.split('=', 1)
print sp
os.environ[sp[0]] = sp[1][:-1]
来源:https://stackoverflow.com/questions/2143785/python-dbus-session-bus-x11-dependency