I have two independent scripts that need to be run all the time and display messages in the terminal: script1.py and script2
To run the scripts I use the code editor VSCode under Windows. In order to use the 2 independent terminals I create them separately and in each of them I write the command: python script1.py and python script2.py
Each time I restart, I have to do it all over again. (yes it's not hard, but I think there is a way that could run both scripts and create separate terminals in VSCode)
What I tried to do:
import subprocess
subprocess.Popen(["python", "script1.py"], creationflags=subprocess.CREATE_NEW_CONSOLE)
subprocess.Popen(["python", "script2.py"], creationflags=subprocess.CREATE_NEW_CONSOLE)
This execution option brings up two separate command-line interpreter windows. This is the usual сmd.exe so if I didn't use VSCode but ran the scripts directly through the Windows console.
import subprocess
p1 = subprocess.Popen(["python", "script1.py"], shell=True)
p2 = subprocess.Popen(["python", "script2.py"], shell=True)
This execution option outputs the result of 2 scripts into one VSCode terminal, which is inconvenient.
In fact, the solution to the problem itself has been written in your question:
In order to use the 2 independent terminals I create them separately
and in each of them I write the command: python script1.py and python
script2.py
This is the most correct and safe method. Of course, if you feel troublesome, I can provide the following alternatives:
Install code-runner extension, then use Run Code button (default shortcuts "Ctrl+Alt+N") to run the python1.py then Run Python file (default shortcuts "Shift+Enter") button to run the python2.py.
Use jupyter-notebook, and create the same codes in python1.ipynb and python2.ipynb. Run the codes in interactive windows is a good choice aswell.
Related
How to get an sh script for starting a new terminal, execute a python script and keep it running? The python script is supposed to run continuously in a perpetual loop, spitting out results as they pop in. Whenever trying with sh-script for gnome-terminal just getting: child process exited normally with status 2
Manually it would just be: python home/ubuntu/pyscript.py
Could someone give an idea how to do this?
I have a list of scripts to run, so resorting to the manual solution is tedious.
You can use gnome-terminal with the -x flag.
Suppose you have a spam.py script; then the following command will spawn a new terminal, run spam.py in it, and close the terminal once the script has ended.
gnome-terminal -x python spam.py
Try with this script:
# spam.py
import time
for _ in range(5):
print("eggs")
time.sleep(1)
Then the previous command will spawn a terminal, that will be printed eggs five times, and then will be closed.
If you want to leave the terminal open with the Python interpret still running after the script ended, then Python's -i flag (doc then CTRL+F -> -i) is what you want:
gnome-terminal -x python -i spam.py
To run the Python script in a new instance of your favourite terminal, write:
x-terminal-emulator -e python -i home/ubuntu/pyscript.py
This will start the Python script and run it until it ends, then display a Python prompt to stop the terminal emulator from closing.
This will work with x-terminal-emulator substituted with any of the many, many terminals installed on my computer, so will work with little modification across all POSIX-compatible systems with the standard terminals installed. This won't work on a Mac, however. For a properly cross-platform Python implementation of something slightly different, see here. Most of the techniques should be transferable.
To run the Python script in the same terminal whilst carrying on with the rest of the shell script, write:
python home/ubuntu/pyscript.py &
Note the &, which runs the program as a new process (but still connects the output to the virtual terminal).
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\" &")
I am trying to set up daemontools with a large python program that spawns various subprocesses, and I'm having issues where the subprocesses are not spawning correctly. The subprocess just appears as a zombified process when launched via daemontools.
I have provided a simplified example to demonstrate this.
/service/test/run:
#!/bin/sh
cd /script_directory/
exec envdir /service/test/env /usr/bin/python3 test_subprocess.py
/script_directory/test_subprocess.py
import subprocess
from time import sleep
subprocess.Popen("xterm")
while True:
sleep(1)
test_subprocess.py simply launches a GUI terminal and stays alive, so I can see if it is still running in top/htop.
If I run the script either as root or a non-root user, the script properly executes and the window is displayed. When run via daemontools/supervise, the xterm is zombified and no window is shown.
Setting the env/DISPLAY and env/XAUTHORITY variables as described here doesn't seem to work for me.
On further investigation, the subprocess is zombified even if it does not use the GUI. For example if the subprocess in subprocess.py is "top" - it will not run.
I've used daemontools successfully on various other projects that don't spawn subprocesses so I don't think the issue is with the basic setup here.
Can daemontools be used with scripts that spawn other processes?
If not, what some other recommended tools for daemonising complex python applications?
bro i can't understand what you went to do. but try this program:
import subprocess
p = subprocess.Popen(
['xterm', '-hold'], stdin=subprocess.PIPE)
p.communicate()
if went to give some argument use -e and type command,and if another problem please let me know.thanks
I need to execute the simple command below in windows 7 command prompt using Python26.
cd C:\Python26\main project files\Process
C:\Aster\runtime\waster Analysis.comm
It runs a FEM simulation and I tried it manually and it worked well. Now, I want to automate the write procedure using Python26.
I studied the other questions and found that the os.system works but it didn't. Also I saw subprocess module but it didn't work.
The current directory is a process property: Every single process has its own current directory. A line like
os.system("cd xyz")
starts a command interpreter (cmd.exe on Windows 7) and execute the cd command in this subprocess, not affecting the calling process in any way. To change the directory of the calling process, you can use os.chdir() or the cwd keyword parameter to subprocess.Popen().
Example code:
p = subproces.Popen(["C:/Aster/runtime/waster", "Analysis.comm"],
cwd="C:/Python26/main project files/Process")
p.wait()
(Side notes: Use forward slashes in path names in Python files. You should avoid os.system() and passing shell=True to the function in the subprocess module unless really necessary.)
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.