I want to launch a python script in a new macOS terminal window from another script. I'm currently using this code:
subprocess.call(['open', '-a', 'Terminal.app', '/usr/bin/python'])
which launches a python prompt in a new terminal window.
But when I try to run a python script with this code:
subprocess.call(['open', '-a', 'Terminal.app', '/usr/bin/python', 'test.py'])
it completely ignores the 'test.py' on the end and starts a python prompt, just like it does without the test.py on the end.
How can I make this work?
Don't use the open -a terminal.app, just use the python executable
subprocess.call(['/usr/bin/python', 'test.py'])
This Python code will open a new terminal window, and then have python3 run test.py:
import os
os.system("""osascript -e 'tell application "Terminal" to do script "python3 test.py"'""")
I'm unable to come up with a way to get this into a subprocess.call() call.
Related
I am attempting to get a subprocess call that will open a gnome-terminal and in that terminal enter python then execute some python commands and imports without the user needing to type them out.
I'm working on on some automated terminal opening code that will open a gnome-terminal window using subprocess.call (Open new gnome-terminal and run command) also (Python syntax to open gnome-terminal and execute multiple commands)
My end goal is to open up a gnome-terminal window and with the same script that opened the gnome-terminal, enter the command to use python. And then in python import a package and run it.
My current usage is:
subprocess.call(['gnome-terminal', '-e', "python client.py"])
However what Im trying to get to is an importable package that I can open several gnome terminal windows for that will call different objects from a pypi package effectively doing the same thing that call client.py would do with the files. This doesnt work with packages installed in pip however.
What I want to do is something along the lines of:
subprocess.call(['gnome-terminal', '-e', "python && import <package> && c = <package>.obj.func()"])
So that a terminal would open and enter python, import the package I want, then call something from it, but all as instructed by a python file
This doesnt appear to work as multiline scripting works for stuff like bash scripting but doesnt seem to work when trying to enter commands after python has been entered.
Any advice would be greatly appreciated
I don't have Gnome Terminal installed, but if you can get that to start Python correctly, then you can use Python's -i flag to run a set of commands or a script.
The two usages are as follows:
python -i path/to/my/script run the script then enter the interpreter
python -i -c "# Some Python commands" run the command(s) then enter the interpreter
For example:
$ python -i -c "import this"
[poetry]
>>>
# Ready for input!
OS: Raspbian
Python: 3.7.3
I am trying to run and kill my shell script through a Python script. The purpose is so that I can simply press "Run" in my py script, instead of having to go through the terminal every time. Here is my shell script (T.sh):
#!/bin/bash
#Track command
cd /home/pi/rpi-deep-pantilt
. . /rpi-deep-pantilt-env/bin/activate
rpi-deep-pantilt track Raspi --edge-tpu
Here is my Py script:
import os
os.system('bash /home/pi/T.sh')
When I issue the command rpi-deep-pantilt track Raspi --edge-tpu in my terminal and press CTRL + C it kills the script, but it doesn't work when I use this Python script, and neither does pkill. The Python script stops, but the camera stays on and the tracking functionality is still operating. Is there any way I can incorporate some kill command that I can issue with a key interruption?
If there is a better way to go about this let me know. I'm very new to this as you can probably tell.
Python may create a new process for T.sh, so within your python code, try:
os.system("pkill T.sh")
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.
Is it possible to run shell command at background in python script? Let say I have run.py and in this script I need to do,
#run shell command(in python script until script is finished)
#continue with script
But the problem is that they are dependent. To run my script I need to run shell command in the same script.
Yes, you can use the subprocess library in Python and check the status of the output.
Ex: subprocess.check_call("exit 1", shell=True)
I have to execute python script in windows command prompt
I am using the following command to run the command, so that the script opens the command prompt execute it
os.system("start /wait cmd /c {c:\\python27\\python.exe C:\\examples\\xml2html.py --dir c:\\Temp\\abcd c:\\tmp\\results.xml}")
I will be expecting a new directory called "abcd" created at that location and some output files created inside that.
When I run this command normally in the windows command prompt it works. I am not able to execute this in the script. Windows command prompt opens and terminates quickly.
Could any one let me know where exactly is it going wrong with the command please?
Unless you want to open a new console window you don't need to run cmd.exe (%COMSPEC%) in order to run another Python script as a subprocess:
import sys
from subprocess import check_call
check_call([sys.executable, "C:\\examples\\xml2html.py",
"--dir", "c:\\Temp\\abcd", "c:\\tmp\\results.xml"])