in my script, i call an app from os.system()
command = f"python -m snakeviz {filename}"
os.system(command)
the problem is that this application is freezing the console, therefore freezing my python plugin.
we cannot input anything, ("exit" command won't work)
the only way to close the terminal is by running CTRL+C shortcut, and the terminal might be hidden..
this app has no reason to keep running it just open a webpage,
how can I force quit the command, without losing the rest of my script?
Whatever i did, the command froze my python plugin
I needed to open another process with Open()
and need shlex to encrypt the command line to whatever this subprocess module need
command = f"python -m snakeviz myfile.log"
subprocess.Popen(shlex.split(command))
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!
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.
I've been spending the last week or so creating a simple touch friendly GUI in python3.4 (on the raspberry pi). Now I setup python to run my script on launch, but I ran into the problem, that I couldn't open other programs from within my program (such as the web browser or calculator). However if I use IDLE to execute the script and not the standard python program in the terminal, opening other programs from my scrip works! I already created a .sh file that runs when the Linux Gui starts, which opens up my script in IDLE, however it only opens the file and doesn't execute it.
So now here is my question: Can I create a .sh script, which opens IDLE and runs a python script in the IDLE console (I already tried the exec command when launching idle with no results)
Right now this is my command, which should execute the loaded file, but only loads it for some reaseon:
sudo idle3 -c exec(open('/path/to/my/file.py').read())
Any help is appreciated :)
Use Idle's cli options
You have a few options, of which the best one is to use the -r option. From man idle:
-r file
Run script from file.
This will however only open the interpreter window. Since you also want the editor, this will do pretty much exactly what you describe:
idle3 '/path.to/file.py' & idle3 -r '/path.to/file.py'
The startup command you need is then:
/bin/bash -c "idle3 '/path/to/file.py' & idle3 -r '/path/to/file.py'"
The command you tried will not work, since here, we can read:
Only process 0 may call idle(). Any user process, even a process with superuser permission, will receive EPERM.
Therefore, we depend on cli options of idle, which luckily provide an option :)
Alternatively
Another option would be to open the file with idle3 wait for the window to appear and simulate F5:
/bin/bash -c "idle3 '/path/to/file.py' & sleep 3 && xdotool key F5"
This would need xdotool to be installed on your system.
An advanced version of this wrapper would open the file with idle, subsequently check if the new window exists, is focussed and simulate F5 with xdotool.
These would however be the dirty options, which we luckily don't need :).
I have what seems to be a simple use case: I launch a script (python or bash) which runs an emulator from command prompt and then the emulator takes commands until I type ctrl-c or exit. I want to do this same thing from a shell and my code below isn't working. What I am trying to do is test automation so I want to issue commands directly to the application from command shell. In python, I have the following:
import os
import subprocess
command = ['/usr/local/bin/YCTV-SIM.sh', '-Latest'] #emulator for yahoo widgets
process = subprocess.Popen( command, shell=True, stdin=subprocess.PIPE )
time.sleep(12) #wait for launch to finish
print '/widgets 1' #first command to issue
print '/key enter' #second command to issue
process.wait()
As you can see, this is some pretty simple stuff. When 'YCTV-SIM.sh' is launched from the command shell, I am put into an input mode and my key entries are sent to the application shell (YCTV-SIM.sh reads raw input) so ideally, I would be able to pipe text directly to this application shell. So far tho, nothing happens; test outputs to the console window but the application does not respond to the commands that I attempt to issue. I am using python 2.6.3, if that matters, but Python is not required..
Language is immaterial at this point so PERL, Python, Bash, TCL... whatever you can suggest that might help.
You need to redirect stdin of the child process and write into it. See e.g. subprocess.Popen.communicate.
I would like to run the specific commandline application:
ffmpeg -i video.mp4 audio.mp3
I'm running the command through a GUI, and when the console window doesn't exist, the ffmpeg process is running in a new cmd window.
Testers find the "black window that appears" scary and not userfriendly.
How can I run the application without any visible window coming up? os.system(), subprocess.Popen() and subprocess.call() all do launch the cmd window.
If it matters, I'm using pyqt4 and py2exe. I'm targeting Windows OS users.
This recipe at ActiveState may solve your problem:
http://code.activestate.com/recipes/409002/
Slight changes are required for Python 2.7. See How do I eliminate Windows consoles from spawned processes in Python (2.7)?
Launch ffmpeg from the START command. If you use the /B switch, no command window will be shown.
Use subprocess.Popen (or call) and redirect stdout/stderr somewhere. They're currently hooked to your own process's stdout and stderr, which is why they're coming through.
If you need something that can integrate nicely with your GUI event loop, use Twisted's process-launching stuff.