run python program one by one - python

Is possible to run bunch of python based program one by one in python terminal? I have number of program that is executed in Python. I want to make a single file, so that I can just run a single python file, and it will execute each program one by one in that file.
Single file:some_files.py
\home\something\1.py
\home\something\2.py
\home\something\3.py
\home\something\4.py

There are two very easy ways to achieve the same goal, without using a python script nor python terminal (I'm aware that's not exacly what you're asking, but it's very easy).
An IPython script
run_all.ipy (.ipy is the extension for ipython scripts)
%run \home\something\1.py
%run \home\something\2.py
%run \home\something\3.py
%run \home\something\4.py
A shell/batch script
(on windows use a batch file (.bat) instead)
run_all.sh
python \home\something\1.py
python \home\something\2.py
python \home\something\3.py
python \home\something\4.py

You can use the suprocess build-in library.
import subprocess
prog_max = 10
for i in range(prog_max):
s = subprocess.Popen(['python','%i.py'%i], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
out,_ = s.communicate()

I use more simple approch in Subprocess module,
import os
os.chdir('\home\something\')
import subprocess`
subprocess.call(['python','1.py'])
subprocess.call(['python','2.py'])

Related

Is it possible to code something for automatically run multiple python files consecutively in cmd

I have some codes that will take time to run for a while. Is it possible that I create a new file that has a code like
import run001
import run002
import run003
and run this in cmd to make it automatically run other three .py files (run001.py, run002.py, run003.py) from the same folder? This import way works only the first file but not automatically continue the second one.
Yes we can call a command in a python script directly with the use of either
subprocess module or os.system. Here is the link for official documentation.
subprocess
os.system
Though i would suggest you to stick to subprocess module as it is newer and provides more control to user on the other hand os.system just spawns a process and returns the return code
subprocess module allows us to spawn new process through our Python script, so let us assume you want to list all the files in your directory, this can be achieved by a simple call to subprocess.call()
import subprocess as sub
sub.call(yourCommand) #to pass a single command
sub.call(commandList) #to pass a multiple commands

Python script to run command line which starts python script with specific python version

I need some help. Is there a possibility to let python start the command line in windows and let the command line execute a script in another python version on my pc?
Expample: I have two versions of python on my pc. One is within Anaconda and the other one is pure Python. Now I have some scripts I want to be executed in specific order. My problem is, that the Google Analytics API doesn't work with Anaconda and some other packages (like Simpy) doesn't work with pure Python. So I need to work with two different versions of python for one project.
Now I want to write a litte python file, which opens the command line and executes the scrips in specific order on my different Python-versions.
I know how to run a python file on the command line. It's via
C:\path_to_python\python.exe C:\path_to_file\file.py
But how can I make a python script executing that line above in the command line?
Hope someone can help me.
Thanks.
import os
os.system("C:\path_to_python\python.exe C:\path_to_file\file.py")
os.system() returns the command's exit value so if you need some output from the script this won't work.
I suggest you look at subprocess
# this is new to python 3.5
import subprocess
cmd = subprocess.run(["C:/path_to_python/python.exe", "C:/path_to_script/script.py"], stdout=subprocess.PIPE)
return_string = cmd.stdout
# alternative for getting command output in python 3.1 and higher
import subprocess
return_string = subprocess.check_output(["C:/path_to_python/python.exe", "C:/path_to_script/script.py"])
Instead you can try writing a batch file in which you can specify the order how you want to run the files and with which version you have to run the file.
lets say first i want to run a file in python2.7 and the later in python3.4 and my files were in d:/pythonfiles
RunningSequence.bat
d:
cd D:\pythonfiles
c:\python27\python.exe python27file.py
c:\python34\python.exe python34file.py
try this and let me know :
import sys
with open(sys.argv[1], 'r') as my_file:
exec(my_file.read())

How to start child cmd terminals in separate windows, from python script and execute scripts on them?

I have been trying rather unsuccesfully to open several terminals (though one would be enough to start with) from say an ipython terminal that executes my main python script. I would like this main python script to open as many cmd terminals as needed and execute a specific python script on each of them. I need the terminal windows to remain open when the script finishes.
I can manage to start one terminal using the command:
import os
os.startfile('cmd')
but I don't know how to pass arguments to it, like:
/K python myscript.py
Does anyone have any ideas on how this could be done?
Cheers
H.H.
Use subprocess module. Se more info at. Google>>python subprocess
http://docs.python.org/2/library/subprocess.html
import subprocess
subprocess.check_output(["python", "c:\home\user\script.py"])
or
subprocess.call(["python", "c:\home\user\script.py"])

Why does running a PowerShell script from Python seem to delay?

When I launch a PowerShell script from Python, the delay seems to be approximately 45s, and I cannot figure out why.
I'm trying to run a PowerShell script (accessing some APIs only available to PowerShell) from a Python script.
I've tried a lot of permutations, and all incur ~45 second delay compared to just running the script from a command prompt, using an identical command line.
For example - sample.ps1 might say:
echo foo
And runner.py might say:
import subprocess
p = subprocess.Popen([POWERSHELL, '-File', 'sample.ps1'], stdout=subprocess.STDOUT)
d = p.stdout.read()
Running the .ps1 script directly is fast, running it via runner.py (Python 2.7, 32bit on a 64bit machine) incurs 45 second delay.
The exact same thing occurs if I use "os.system", or Twisted's built-in process tools. So I suspect it's some subtle interaction between the Python interpreter and the Powershell interpreter, possibly related to creation of console windows, or handling of stdin/out/err streams? (which I know don't "really exist" in the same way on Windows)
I do not see any such delays. It is pretty snappy. ( that will also depend on what your script actually does.) Try using call:
from subprocess import call
call(["powershell", "sample.ps1"])
PowerShell loads your user's profile by default. Use the -NoProfile argument to turn that behavior off:
import subprocess
p = subprocess.Popen([POWERSHELL, '-NoProfile', '-File', 'sample.ps1'], stdout=subprocess.STDOUT)
d = p.stdout.read()

Is there a possibility to execute a Python script while being in interactive mode

Normally you can execute a Python script for example: python myscript.py, but if you are in the interactive mode, how is it possible to execute a Python script on the filesystem?
>>> exec(File) ???
It should be possible to execute the script more than one time.
Use execfile('script.py') but it only work on python 2.x, if you are using 3.0 try exec(open('script.py').read())
import file without the .py extension will do it, however __name__ will not be "__main__" so if the script does any checks to see if it's being run interactively you'll need to bypass them.
Alternately, if you're wanting to have a look at the environment after the script runs try python -i script.py
EDIT: To load it again
file = reload(file)
You might want to look into IPython, a more powerful interactive shell. It has various "magic" commands including %run script.py (which, of course, runs the script and leaves any variables it defined for you to examine).
You can also use the subprocess module. Something like:
>>> import subprocess
>>> proc = subprocess.Popen(['./script.py'])
>>> proc.communicate()
You can run any system command using python:
>>>from subprocess import Popen
>>>Popen("python myscript.py", shell=True)
The easiest way to do it is to use the os module:
import os
os.system('python script.py')
In fact os.system('cmd') to run shell commands. Hope it will be enough.

Categories

Resources