zl程序教程

您现在的位置是:首页 >  Python

当前栏目

linux下使用python打开terminal时报错

2023-04-18 15:45:22 时间

场景一:在jenkins上执行自动化任务,测试任务执行时使用jenkins用户(有sudo权限),测试脚本在linux执行(后台),执行到以下代码时报错:

os.system(f"/usr/bin/konsole -e {cmd_orin2_10s}") 

场景二:测试脚本在本地执行没有问题,本地执行时使用root用户,可正常打开命令窗口

错误日志

17:44:03  qt.qpa.xcb: could not connect to display 
17:44:03  qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
17:44:03  This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
17:44:03  
17:44:03  Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, xcb.

解决方法

从场景1和场景2可对比出,应该是由环境变量引起。从环境变量已经显示窗口这两方面去查找问题,可找到 export DISPLAY=":0.0"

参考文档:

《export DISPLAY=":0.0"》