Launching a python script in a new terminal from another python script - python

I'm using LXDE, I would like to Launch a python script in a new terminal from another python script.
I would like the new python script to be totally independent.
I have tried a lot of things...
Calling xterm (or x-terminal-emulator) directly from python with the subprocess.call or subprocess.Popen with or without shell=True argument, it didn't work. It gives me an error about display not being set.
I have also created a sh file which calls the other python script and tried to call it, same results.
Any way to do this?

This works fine for me:
blocking:
import os
os.system("xterm -e \"python christmaskittens.py\"")
non blocking:
import os
os.system("xterm -e \"python christmaskittens.py\" &")

Related

Launching python3 script using a python2 script

is there a way to launch a script running on python3 via a python2 script.
To explain briefly I need to start the python3 script when starting the python2 script.
Python3 script is a video stream server (using Flask) and have to run simultaneously from the python2 script (not python3 script first and then python2 script).
The ideal would be to get a function in the python2 script which "open a cmd window and write" it in : python3 script_python3.py
create command in bash file. command is to run script_python3.py, ex: python script_python3.py
then from python2 file, run the .bash file. ex:
import os
os.popen('sh /scripts/my_bash.sh')
You could use something like explained in this answer.
That use case of that question is a bit different, but the answer should work.

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

How to spawn detached background process on Linux in either bash or python

I have a long running python script on Linux, and in some situations it needs to execute a command to stop and restart itself. So, I would like to have an external script (either in bash or python) that executes command to restart the original script. Let me elaborate.
Suppose I have original_script.py. In original_script.py I have this in an infinite loop:
if some_error_condition:
somehow call external script external.sh or external.py
Let's suppose I can call external.sh and it contains this:
#!/bin/bash
command_to_restart_original_script
Finally, I know the command "command_to_restart_original_script". That isn't the problem. What need is the python command to "somehow call external script external.sh". I need the external script (which is a child process) to keep running as the parent process original_script.py is restarting, ie I need the child process to be detached/daemonized. How do I do this?
I found lots of suggestions in various places, but the only answer that worked for me was this:
How to launch and run external script in background?
import subprocess
subprocess.Popen(["nohup", "python", "test.py"])
In my case I ran a script called longrun.sh so the actual command is:
import subprocess
subprocess.Popen(["nohup", "/bin/bash", "longrun.sh"])
I tested this using this run.py:
import subprocess
subprocess.Popen(["nohup", "/bin/bash", "longrun.sh"])
print "Done!"
and I verified (using ps -ax | grep longrun) that longrun.sh does indeed run in the background long after run.py exits.

Ansicon doesn't install from python but only from cmd

I'm trying to install ansicon on Windows 8.1. I extracted the files and got to the level that I need to call ansicon -i. When I type this in my cmd and run python scripts that works great but when I call t from python by os.system('ansicon -i') that doesn't work and seems like it doesn't have any influence on the cmd.
Why os.system('ansicon -i') doesn't work and what alternative method can I use from within python?
First off, it’s not the -i flag that really does the work. -i only tells it to add itself to AutoRun. The -p flag that -i implies is what really does the work: -p tells it to inject a DLL into the parent process, and therein lies the problem: when you use os.system, you spawn a shell, which then runs the command you give it. But then you have Python running cmd running ansicon, and ansicon will inject into cmd, and then cmd, having finished its work, will exit.
Rather than using os.system, use the subprocess module, e.g.:
subprocess.check_call(['ansicon', '-p'])
The subprocess module (unlike os.system) will execute the command directly without a shell in-between (unless you pass shell=True). Then Python will spawn ansicon, and ansicon will inject into Python, as desired.
That said, rather than having ansicon inject itself into Python, Python could probably just load the DLL itself, avoiding some hardship:
import sys
import math
import ctypes
bitness = 1 << round(math.log2(round(math.log2(sys.maxsize + 1))))
ctypes.WinDLL('ANSI{}.DLL'.format(bitness))

os.system doesn't work in Python

I'm working on windows vista, but I'm running python from DOS command. I have this simple python program. (It's actually one py file named test.py)
import os
os.system('cd ..')
When I execute "python test.py" from a Dos command, it doesn't work.
For example, if the prompt Dos Command before execution was this:
C:\Directory>
After execution, must be this:
C:\>
Help Plz.
First, you generally don't want to use os.system - take a look at the subprocess module instead. But, that won't solve your immediate problem (just some you might have down the track) - the actual reason cd won't work is because it changes the working directory of the subprocess, and doesn't affect the process Python is running in - to do that, use os.chdir.
I don't really use Windows, but you can try cmd /k yourcommandhere. This executes the command and then returns to the CMD prompt.
So for example, maybe you can do what you want like this:
subprocess.call(['cmd', '/k', 'cd .. && prompt changed'])
As I said, I am not familiar with Windows, so the syntax could be wrong, but you should get the idea.
In case you don't know, this is a different CMD instance than the one you were in before you started your python script. So when you exit, your python script should continue execution, and after it's done, you'll be back to your original CMD.

Categories