Python: looping a python script in another python script with parameter passing - python

I am trying to run a py script in loop from another py file with a parameter passing.
I am trying the following:
Script1:
lst = [12,23,45,67,89]
age_lst = []
for i in lst:
age_i = os.system("python script_to_run {0}".format(int(i)) )
age_lst.append(age_i)
Below is the code for script_to_run.py
Script2:script_to_run.py
def age(age:int):
estimated_val = age+2
return estimated_val
if __name__=="__main__":
my_age = int(sys.argv[1])
final_age = age(age=my_age)
print(final_age)
Whenever I am running Script 1 where I am calling Script 2 (script_to_run.py) It is running fine but age_lst[] is being populated only with 2.
Expectation is
age_lst = [14,25,47,69,91] <---adding 2 with all elements in age_lst
What I am missing?
Also when I am running the Script1.py from cmd, I am getting error python: can't open file 'script_to_run': [Errno 2] No such file or directory
I am using Windows 10.

os.system runs the program and returns its exit code. Your script writes to standard output, a different beast entirely. Exactly what returns from os.system is OS dependent. On linux for example, its the exit code, limited to 0-255, shifted left with signal information added. Messy.
But since you are converting the output to a string and printing to stdout anyway, just have the parent process read that. The subprocess module has several functions that run programs. run is the modern way.
import subprocess as subp
import sys
lst = [12,23,45,67,89]
age_lst = []
for i in lst:
proc = subp.run([sys.executable, "script_to_run.py", str(i)],
stdout=subp.PIPE)
if proc.returncode == 0:
print("script returned error")
else:
age_lst.append(int(proc.stdout))
print(age_lst)

Related

Cant connect ubuntu terminal and python script

Im trying to use ubuntu terminal through python script.
The script receives the command text(from input() for test), sends it to the terminal, the terminal return the result of the command, script print result in a console.
I already get that I should use subprocess Popen and PIPE, but after inputing 1st command, script print only b'', after 2nd raise error "ValueError: Cannot send input after starting communication"
my test code(yes, its bad):
import subprocess as sb
from subprocess import PIPE, Popen
p=Popen(['gnome-terminal'],stdout=PIPE,stdin=PIPE,stderr=PIPE)
command = 'cmd'
while True:
command = input()
out_data, err_data = p.communicate(command.encode())
print(repr(out_data))
I know I do it in a wrong way, but can't find right. Sorry for English.Thanks.
You can do this using the os module:
#!/usr/bin/env python
import os
output = os.listdir('path_to_folder') # output is a list
# Do whatever you want to output
You can read more about what else the os module can do at https://docs.python.org/3/library/os.html. Note that the methods in the module are portable across different OSes, so you can in fact use your script outside Ubuntu.

How to launch a couple of python scripts from a first python script and then terminate them all at once?

I have a function in a python script which should launch another python script multiple times, I am assuming this can be done like this(Script is just my imagination of how this would work.)
iterations = input("Enter the number of processes to run")
for x in range(0, iterations):
subprocess.call("python3 /path/to/the/script.py", shell=True)
but, I also need to pass over some defined variables into the other script, for example, if
x = 1
in the first script, then, I need x to have the same value in the second script without defining it there, I have NO idea how to do that.
And then also killing them, I have read about some method using PIDs, but don't those change every time?
Most of the methods I found on Google looked overly complex and what I want to do is really simple. Can anyone guide me in the right direction as to what to use and how I should go at accomplishing it?
I have a function in a python script which should launch another python script multiple times, I am assuming this can be done like this(Script is just my imagination of how this would work.)
**
Here is the subprocess manual page which contains everything I will be talking about
https://docs.python.org/2/library/subprocess.html
One of the way to call one script from other is using subprocess.Popen
something on the lines
import subprocess
for i in range(0,100):
ret = subprocess.Popen("python3 /path/to/the/script.py",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
you can use the return value from Open to make the call synchronous using the communicate method.
out,err = ret.communicate()
This would block the calling script until the subprocess finishes.
I also need to pass over some defined variables into the other script??
There are multiple ways to do this.
1. Pass parameters to the called script and parse it using OptionPraser or sys.args
in the called script have something like
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-x","--variable",action="store_true",dest="xvalue",default=False)
(options,args) = parser.parse_args()
if options.xvalue == True:
###do something
in the callee script use subprocess as
ret = subprocess.Popen("python3 /path/to/the/script.py -x",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
Note the addition of -x parameter
You can use args parse
https://docs.python.org/2/library/argparse.html#module-argparse
Pass the subprocess a environment variable which can be used to configure the subprocess. This is fast but this only works one way, i.e. from parent process to child process.
in called script
import os
x = int(os.enviorn('xvalue'))
in callee script set the environment variable
import os
int x = 1
os.environ['xvalue'] = str(x)
Use sockets or pipes or some other IPC method
And then also killing them, I have read about some method using PIDs, but don't those change every time?
again you can use subprocess to hold the process id and terminate it
this will give you the process id
ret.pid
you can then use .terminate to terminate the process if it is running
ret.terminate()
to check if the process is running you can use the poll method from subprocess Popen. I would suggest you to check before you terminate the process
ret.poll()
poll will return a None if the process is running
If you just need to pass some values to second script, and you need to run that
by means of subprocess module, then you may simply pass the variables as command line arguments:
for x in range(0, iterations):
subprocess.call('python3 /path/to/second_script.py -x=%s'%x, shell=True)
And recieve the -x=1 via sys.argv list inside second_script.py (using argparse module)
On the other hand, If you need to exchange something between the two scripts dynamically (while both are running), You can use the pipe mechanism or even better, use the multiprocessing (wich requires some changes in your current code), it would make communication with and controlling it (terminating it) much cleaner.
You can pass variables to subprocesses via the command line, environment variables or passing data in on stdin. Command line is easy for simple strings that aren't too long and don't themselves have shell meta characters in them. The target script would pull them from sys.argv.
script.py:
import sys
import os
import time
x = sys.argv[1]
print(os.getpid(), "processing", x)
time.sleep(240)
subprocess.Popen starts child processes but doesn't wait for them to complete. You could start all of the children, put their popen objects in a list and finish with them later.
iterations = input("Enter the number of processes to run")
processes = []
for x in range(0, iterations):
processes.append(subprocess.Popen([sys.executable, "/path/to/the/script.py", str(x)])
time.sleep(10)
for proc in processes:
if proc.poll() is not None:
proc.terminate()
for proc in processes:
returncode = proc.wait()

Infinite while not working with os.execvp

I am programming in python which involves me implementing a shell in Python in Linux. I am trying to run standard unix commands by using os.execvp(). I need to keep asking the user for commands so I have used an infinite while loop. However, the infinite while loop doesn't work. I have tried searching online but they're isn't much available for Python. Any help would be appreciated. Thanks
This is the code I have written so far:
import os
import shlex
def word_list(line):
"""Break the line into shell words."""
lexer = shlex.shlex(line, posix=True)
lexer.whitespace_split = False
lexer.wordchars += '#$+-,./?#^='
args = list(lexer)
return args
def main():
while(True):
line = input('psh>')
split_line = word_list(line)
if len(split_line) == 1:
print(os.execvp(split_line[0],[" "]))
else:
print(os.execvp(split_line[0],split_line))
if __name__ == "__main__":
main()
So when I run this and put in the input "ls" I get the output "HelloWorld.py" (which is correct) and "Process finished with exit code 0". However I don't get the output "psh>" which is waiting for the next command. No exceptions are thrown when I run this code.
Your code does not work because it uses os.execvp. os.execvp replaces the current process image completely with the executing program, your running process becomes the ls.
To execute a subprocess use the aptly named subprocess module.
In case of an ill-advised programming exercise then you need to:
# warning, never do this at home!
pid = os.fork()
if not pid:
os.execvp(cmdline) # in child
else:
os.wait(pid) # in parent
os.fork returns twice, giving the pid of child in parent process, zero in child process.
If you want it to run like a shell you are looking for os.fork() . Call this before you call os.execvp() and it will create a child process. os.fork() returns the process id. If it is 0 then you are in the child process and can call os.execvp(), otherwise continue with the code. This will keep the while loop running. You can have the original process either wait for it to complete os.wait(), or continue without waiting to the start of the while loop. The pseudo code on page 2 of this link should help https://www.cs.auckland.ac.nz/courses/compsci340s2c/assignments/A1/A1.pdf

input() blocks other python processes in Windows 8 (python 3.3)

Working on a multi-threaded cross-platform python3.3 application I came across some weird behavior I was not expecting and am not sure is expected. The issue is on Windows 8 calling the input() method in one thread blocks other threads until it completes. I have tested the below example script on three Linux, two Windows 7 and one Windows 8 computers and this behavior is only observed on the Windows 8 computer. Is this expected behavior for Windows 8?
test.py:
import subprocess, threading, time
def ui():
i = input("-->")
print(i)
def loop():
i = 0
f = 'sky.{}'.format(i)
p = subprocess.Popen(['python', 'copy.py', 'sky1', f])
t = time.time()
while time.time() < t+15:
if p.poll() != None:
print(i)
time.sleep(3)
i+=1
f = 'sky.{}'.format(i)
p = subprocess.Popen(['python', 'copy.py', 'sky1', f])
p.terminate()
p.wait()
def start():
t1 = threading.Thread(target=ui)
t2 = threading.Thread(target=loop)
t1.start()
t2.start()
return t2
t2 = start()
t2.join()
print('done')
copy.py:
import shutil
import sys
src = sys.argv[1]
dst = sys.argv[2]
print('Copying \'{0}\' to \'{1}\''.format(src, dst))
shutil.copy(src, dst)
Update:
While trying out one of the suggestions I realized that I rushed to a conclusion missing something obvious. I apologize for getting off to a false start.
As Schollii suggested just using threads (no subprocess or python files) results in all threads making forward progress so the problem actually is using input() in one python process will cause other python processes to block/not run (I do not know exactly what is going on). Furthermore, it appears to be just python processes that are affected. If I use the same code shown above (with some modifications) to execute non-python executables with subprocess.Popen they will run as expected.
To summarize:
Using subprocess to execute non-python executable: works as expected with and without any calls to input().
Using subprocess to execute python executable: created processes appear to not run if a a call to input() is made in the original process.
Use subprocess to create python processes with a call to input() in a new process and not the original process: A call to input() blocks all python processes spawned by the 'main' process.
Side Note: I do not have Windows 8 platform so debugging/tests can be a little slow.
Because there are several problems with input in Python 3.0-3.2 this method has been impacted with few changes.
It's possible that we have a new bug again.
Can you try the following variant, which is raw_input() "back port" (which was avaiable in Python 2.x):
...
i = eval(input("-->"))
...
It's a very good problem to work with,
since you are dependent with input() method, which, usually needs the console input,
since you have threads, all the threads are trying to communicate with the console,
So, I advice you to use either Producer-Consumer concept or define all your inputs to a text file and pass the text file to the program.

Determining running programs in Python

How would I use Python to determine what programs are currently running. I am on Windows.
Thanks to #hb2pencil for the WMIC command! Here's how you can pipe the output without a file:
import subprocess
cmd = 'WMIC PROCESS get Caption,Commandline,Processid'
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
for line in proc.stdout:
print line
import os
os.system('WMIC /OUTPUT:C:\ProcessList.txt PROCESS get Caption,Commandline,Processid')
f = open("C:\ProcessList.txt")
plist = f.readlines()
f.close()
Now plist contains a formatted whitespace-separated list of processes:
The first column is the name of the executable that is running
The second column is the command that represents the running process
The third column is the process ID
This should be simple to parse with python. Note that the first row of data are labels for the columns, and not actual processes.
Note that this method only works on windows!
Piping information from sub process commands is not ideal compared to an actual python tool meant for getting processes. Try the psutil module. To get a list of process numbers, do:
psutil.get_pid_list()
I'm afraid you have to download this module online, it is not included in python distributions, but this is a better way to solve your problem. To access the name of the process you have a number for, do:
psutil.Process(<number>).name
This should be what you are looking for. Also, here is a way to find if a specific process is running:
def process_exists(name):
i = psutil.get_pid_list()
for a in i:
try:
if str(psutil.Process(a).name) == name:
return True
except:
pass
return False
This uses the name of the executable file, so for example, to find a powershell window, you would do this:
process_exists("powershell.exe")
I was getting access denied with get_pid_list(). A newer method worked for me on windows and OSX:
import psutil
for proc in psutil.process_iter():
try:
if proc.name() == u"chrome.exe":
print(proc)
print proc.cmdline()
except psutil.AccessDenied:
print "Permission error or access denied on process"

Categories

Resources