How to make python open new Terminal as normal user and not as root user

719 Views Asked by At

Im trying to get python to run a terminal command which will change my desktop wallpaper. Running this command in a normal terminal will change my wallpaper. However when i try to make python run this command in terminal it doesnt work and gives me an error.

I can replicate this error when i open a terminal as root user. I logged in using su and typed my password. Typing in the command then gives me an error and it does not execute. The same when i try to execute the command with python using either the modules os or subprocess.

Is it because my command to change the wallpaper which starts with pcmanfm (which is the window manager) has probblems with its path and when i am root user the path is changed?

How can i make python open a terminal as "normal" user in my case "pi" on my raspberry pi, and run a command in it?

this is the terminal command which changes my desktop wallpaper:

pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg

this is what happens when i run my python code to open a terminal:

root@raspberrypi:/home/pi/Desktop# 

instead of normally when it works:

pi@raspberrypi:~ $ 

here is my python code which is meant to open a new terminal with the command which changes my desktop wallpaper, however i end up as ROOT user as described above and get an error and nothing happens:

import os,random
import subprocess as sub

sub.call('lxterminal -e bash -c "pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg; sleep 3;exec bash"', shell=True)

executing the following from a normal terminal works perfectly:

lxterminal -e bash -c "pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg; sleep 3;exec bash"

this is the result after running the python code, which is the error i always get:

** Message: 12:11:08.734: x-terminal-emulator has very limited support, consider choose another terminal

root@raspberrypi:/home/pi/Desktop# 

So how can i make python open a terminal as user "pi" not as root? or is this not the problem? thanks!

1

There are 1 best solutions below

0
On

The problem was calling the python script as sudo. Opening the python script normally as: Python3 script.py Will open a terminal as a normal user and. change my desktop wallpaper, Whereas sudo python3 script.py opens the terminal as root user and then the command to change wallpaper no longer works.