terminal error when python tries to execute command - python

I am trying to run a terminal command using python but i get a terminal error:
"x-terminal-emulator has very limited support, consider choose another terminal"
I am trying to change my desktop background using python. I have a raspberry pi running latest raspbian.
Running the command directly in lxterminal it works and changes my background:
"pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge_wallpaper.jpg"
and changes my desktop background.
Using os.system("ls -a") works fine. As do other linux system commands such as "mkdir" or "pwd" python has no problem showing me the output in terminal.
I have tried using subprocess call functions nothing has worked so far.
I also have tried making python open a new, different terminal and running the code in the new terminal but also no luck:
os.system('gnome-terminal --command="pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge_wallpaper.jpg"')
Any thoughts guys? Do i have to start a seperate completely new terminal session? Is it a problem with my syntax? Am i using the wrong python command to execute command in a terminal?
import os,random
random_pic = random.choice(os.listdir('/usr/share/rpd-wallpaper/'))
shell_command = ("pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/" + str(random_pic))
os.system(shell_command)
print("success... wallpaper is changed")

Related

Running python code in VS Code terminal hitting Invalid syntax

I save the below snippet into a file, say, stackoverflow.py and go to terminal and run (within my conda env) then everything is fine. But in VS Code, under the same conda env, it hits Invalid Syntax. The white screenshot is the result from Mac Terminal, the black one is from VS Code Terminal. I'm wondering what happened. Thanks
import requests
request = requests.get("https://drive.google.com/uc?export=download&id=0B6ZlG_Eygdj-c1kzcmUxN05VUXM")
with open("survey.zip", "wb") as file:
file.write(request.content)
From the screenshot it seems when you run it in VS code, your command is being executed in python shell (as python code) and not in terminal.
Just make sure you are using the correct shell when running your command. You can see the active shell in the screenshot where it says "2: python" (in the drop down menu).

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

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!
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.

How do i run python file in cmd from vscode

I have a python program which prints long outputs. When i try to run that file in vscode, its interactive window isn't enough to view full output. So is there any way to run python file in cmd from VSCODE?
If you are running windows, VSCode uses Powershell as your terminal by default. If you want to use the command prompt instead, hit ctrl+shift+p, type Shell into the command pallet, select Terminal: Select Default Shell, and change it to Command Prompt. I am not sure this will fix your problem as I think Powershell should display just as much output as the CMD, but if you want to try switching terminals, that will do it. Another option is to try to run it natively in CMD or Powershell, rather than using the VSCode integrated terminal. That might be better if changing terminals doesn't help.
As #Jeremiah said, you can also just run your script with the Cmd prompt, without using vs code. Let's say you have the file 'test1.py' saved as C:\Users\bcubrich\Documents\test1.py, and your python env .exe is saved in C:\python27\ArcGIS10.5\python.exe. I just wrote script that had this in it:
print('worked')
Then just input this into the Cmd prompt
C:\python27\ArcGIS10.5\python.exe C:\Users\bcubrich\Documents\test1.py
And it printed
worked
to the console.
More on running python through cmd console here:
http://www.cs.bu.edu/courses/cs108/guides/runpython.html

run os.system commands on a new terminal- Python 3

I am running a program which allows me to run terminal commands through my Python code which takes input from the user through the command line. This is the part of the code where I open Google-Chrome
import sys
import os
os.system("google-chrome") #I have Ubuntu 16.04
It opens the browser but the problem is that the terminal on which my python code is running becomes the same as the one where Chrome is running which means that I cannot give further input to my Python code. To solve this problem I need the Chrome to run as a process on a different terminal. I tried using subprocess.call("google-chrome", shell=True) but it did not open it on a new terminal.
How do I make the process run on a different terminal?
can this solve your problem?
os.system('gnome-terminal -x chromium-browser')
Use subprocess.popen("command")
Basically, run the subprocess in the background. & is a shell feature. Use popen instead

Run terminal inside VS Code for python script?

I'm on Linux Mint running VSCode and I was somehow able to run a terminal not as a separate window but right below an open Python file. Seems to be easy on Win/OSX (Ctrl/Cmd+J and select Terminal tab) but not specifically a feature that I can choose when I'm on a Linux machine. Any special keys to bring it back?
In general, Ctrl-` opens the integrated terminal in VS Code. Otherwise look under the View menu for the Integrated Terminal option. If you're looking for Python-specific options, there are Run Python File in Terminal and Run Python Selection/Line in Terminal commands from the command palette.

Categories