场景一:在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"
参考文档:
很多时候你用 pycharm 用习惯了,啥都弄好了,但是却忽略了很多路径上的问题,偶尔需要和其他环境、平台交互的时候,比如写了一个 C# 程序去调用 python 的借口,就相当于 terminal 传参数启动,这个时候就会显示各种各样的 No module 问题 解决办法 究其原因,是因为外部环境在调用 python 接口的时候,要去你的程序中找各个 module,但是如果这些 module 还没有加入到环境变量中,那么就会找不到,因此推荐用下面的语句放在每个用到的 py 文件的最前头
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../')
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../')
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + './')
以上就是解决linux下使用python打开terminal时报错的问题的详细内容,更多关于解决linux下使用python打开terminal时报错的问题的资料请关注九品源码其它相关文章!