Home >Backend Development >Python Tutorial >How to solve the error when opening terminal using python under linux
Scenario 1: Execute automated tasks on jenkins, use the jenkins user (with sudo permissions) when executing test tasks, and the test script is in linux When executing (background), an error occurs when the following code is executed:
os.system(f"/usr/bin/konsole -e {cmd_orin2_10s}")
Scenario 2: There is no problem with the test script being executed locally. When executing locally, the root user can be used and the command window can be opened normally
Error log:
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.
Solution:
It can be compared from scenario 1 and scenario 2, it should be caused by environment variables cause. To find the problem from the environment variables and the displayed window, you can find export DISPLAY=":0.0"
Many times you are used to using pycharm and have everything done, but you ignore many path problems. Occasionally, you need to interact with other environments and platforms. For example, if you write a C# program to call python, it is equivalent to passing parameters to the terminal to start. At this time, various No module problems will be displayed. The reason is because the external environment calls the python interface. , you have to look for each module in your program, but if these modules have not been added to the environment variables, they will not be found. Therefore, it is recommended to use the following statement at the front of each py file used
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../')
This operation can add the entire upper-level directory of the current file to the environment variable, so that all files under the upper-level directory can be easily called
If you have many directory levels and the current directory is in a deep location, then you can slightly change this command to include all the files and directories you want to use, such as Yes
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../')
Of course if you just want to call all directories under the current folder, then you can use
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + './')
The above is the detailed content of How to solve the error when opening terminal using python under linux. For more information, please follow other related articles on the PHP Chinese website!