Alright, I so here are the facts. I have 2 python scripts and I want Script1 to trigger Script2. I have tried the following ways to do this:
from subprocess import call
call(["python3", "script2.py"])
The dreaded exec call:
exec(open("script2.py").read())
And finally:
os.system("script2.py 1")
So just to make sure I am giving you all the info needed. I want to run script1 first then once it is finished processing I want script1 to trigger script2. Currently no matter what I have tried, I get stuck in a loop where script one, just simply keeps running over and over again.
Any ideas?
Here is the actual code for script1:
import os
"""This looks like it is unnecessary but I can't include its context
in this post. Just know it has an actual purpose."""
input_file = "gs://link_to_audio_file.m4a"
audio = input_file
output_format = os.path.basename(input_file).replace("m4a", "flac")
os.system('ffmpeg -i %s -ar 16000 -ac 1 %s' % (audio,output_format))
os.system("python3 script2.py")
Make sure the first script runs cleanly by itself by commenting out the call to the second script. If it still seems to run forever there's an issue other than trying to call a second script. If you have a IDE, you can step through the code to discover where it hangs. If you're not using an IDE, place print statements in the script so you can see the execution path. Do you possibly have a cyclic call? So the first python script is calling the second and the second python script is in turn calling the first?
When using os.system, I believe you'd need to include python as in
os.system("python script2.py 1")
I can't tell why you're in a loop without seeing the scripts.
I have finally solved this issue! I was actually using an import statement in the second script that was trying to import a variable from the first script, but instead it was importing the entire script, causing it to run in an endless loop. Just like LAS had suggested, nicely done! Thank you all for all your help on this!
I have python script that takes command line arguments. The way I get the command line arguments is by reading a mongo database. I need to iterate over the mongo query and launch a different process for the single script with different command line arguments from the mongo query.
Key is, I need the launched processes to be:
separate processes share nothing
when killing the process, I need to be able to kill them all easily.
I think the command killall -9 script.py would work and satisfies the second constraint.
Edit 1
From the answer below, the launcher.py program looks like this
def main():
symbolPreDict = initializeGetMongoAllSymbols()
keys = sorted(symbolPreDict.keys())
for symbol in keys:
# Display key.
print(symbol)
command = ['python', 'mc.py', '-s', str(symbol)]
print command
subprocess.call(command)
if __name__ == '__main__':
main()
The problem is that mc.py has a call that blocks
receiver = multicast.MulticastUDPReceiver ("192.168.0.2", symbolMCIPAddrStr, symbolMCPort )
while True:
try:
b = MD()
data = receiver.read() # This blocks
...
except Exception, e:
print str(e)
When I run the launcher, it just executes one of the mc.py (there are at least 39). How do I modify the launcher program to say "run the launched script in background" so that the script returns to the launcher to launch more scripts?
Edit 2
The problem is solved by replacing subprocess.call(command) with subprocess.Popen(command)
One thing I noticed though, if I say ps ax | grep mc.py, the PID seem to be all different. I don't think I care since I can kill them all pretty easily with killall.
[Correction] kill them with pkill -f xxx.py
There are several options for launching scripts from a script. The easiest are probably to use the subprocess or os modules.
I have done this several times to launch things to separate nodes on a cluster. Using os it might look something like this:
import os
for i in range(len(operations)):
os.system("python myScript.py {:} {:} > out.log".format(arg1,arg2))
using killall you should have no problem terminating processes spawned this way.
Another option is to use subprocess which has got a wide range of features and is much more flexible than os.system. An example might look like:
import subprocess
for i in range(len(operations)):
command = ['python','myScript.py','arg1','arg2']
subprocess.call(command)
In both of these methods, the processes are independent and share nothing other than a parent PID.
I have a Java program that uses video from a framegrabber card. This program is launched through a python launcher.py.
The easiest way to read the video stream I found, is to make Java read on a named pipe, and this works perfectly. So my session is like:
$ mkfifo videopipe
$ cat /dev/video1>videopipe
and in a second terminal (since the cat command is blocking):
$ python launcher.py
I would like to automate this process. Unfortunately, the result is always the same: the Java application starts (confirmed through a print statement in the java program), but then the terminal stalls and nothing appears, exception or else.
Since this process works manually, I guess I am doing something wrong in the python program. To simplify things, I isolated the piping part:
from subprocess import call, Popen, PIPE, check_call
BASH_SWITCHTO_WINTV = ['v4l2-ctl', '-d /dev/video1', '-i 2', '--set-standard=4']
BASH_CREATE_FIFO_PIPE = ['mkfifo', 'videopipe']
BASH_PIPE_VIDEO = 'cat /dev/video1>videopipe'
def run():
try:
print('running bash commands...')
call(BASH_SWITCHTO_WINTV)
call(BASH_CREATE_FIFO_PIPE)
Popen(['cat', '/dev/video1'], stdout=open('videopipe', 'w'))
except:
raise RuntimeError('An error occured while piping the video')
if __name__ == '__main__':
run()
which when run, outputs:
running bash commands...
Failed to open /dev/video1: No such file or directory
A little help would be very much appreciated :-)
If you're using shell=True, just pass a string:
BASH_PIPE_VIDEO = 'cat /dev/video1 > videopipe'
Currently, cat is passed to the shell as your script, and /dev/video>videopipe is passed to that shell as a literal argument -- not parsed as part of the script text at all, and having no effect since the script (just calling cat) doesn't look at its arguments.
Alternately, to avoid needless shell use (and thus shell-related bugs such as shellshock, and potential for injection attacks if you were accepting any argument from a non-hardcoded source):
Popen(['cat', '/dev/video1'], stdout=open('videopipe, 'w'))
On a note unrelated to your "cat to named pipe" question -- be sure you get your spaces correct.
BASH_SWITCHTO_WINTV = ['v4l2-ctl', '-d /dev/video1', ...]
...uses the name <space>/dev/video1, with a leading space, as the input device; it's the same as running v4l2-ctl "-d /dev/video1" in shell, which would cause the same problem.
Be sure that you split your arguments correctly:
BASH_SWITCHTO_WINTV = ['v4l2-ctl', '-d', '/dev/video1', ...]
I have two scripts, a python script and a perl script.
How can I make the perl script run the python script and then runs itself?
Something like this should work:
system("python", "/my/script.py") == 0 or die "Python script returned error $?";
If you need to capture the output of the Python script:
open(my $py, "|-", "python2 /my/script.py") or die "Cannot run Python script: $!";
while (<$py>) {
# do something with the input
}
close($py);
This also works similarly if you want to provide input for the subprocess.
The best way is to execute the python script at the system level using IPC::Open3. This will keep things safer and more readable in your code than using system();
You can easily execute system commands, read and write to them with IPC::Open3 like so:
use strict;
use IPC::Open3 ();
use IO::Handle (); #not required but good for portabilty
my $write_handle = IO::Handle->new();
my $read_handle = IO::Handle->new();
my $pid = IPC::Open3::open3($write_handle, $read_handle, '>&STDERR', $python_binary. ' ' . $python_file_path);
if(!$pid){ function_that_records_errors("Error"); }
#read multi-line data from process:
local $/;
my $read_data = readline($read_handle);
#write to python process
print $write_handle 'Something to write to python process';
waitpid($pid, 0); #wait for child process to close before continuing
This will create a forked process to run the python code. This means that should the python code fail, you can recover and continue with your program.
It may be simpler to run both scripts from a shell script, and use pipes (assuming that you're in a Unix environment) if you need to pass the results from one program to the other
I have just started with Python. When I execute a python script file on Windows, the output window appears but instantaneously goes away. I need it to stay there so I can analyze my output. How can I keep it open?
You have a few options:
Run the program from an already-open terminal. Open a command prompt and type:
python myscript.py
For that to work you need the python executable in your path. Just check on how to edit environment variables on Windows, and add C:\PYTHON26 (or whatever directory you installed python to).
When the program ends, it'll drop you back to the cmd prompt instead of closing the window.
Add code to wait at the end of your script. For Python2, adding ...
raw_input()
... at the end of the script makes it wait for the Enter key. That method is annoying because you have to modify the script, and have to remember removing it when you're done. Specially annoying when testing other people's scripts. For Python3, use input().
Use an editor that pauses for you. Some editors prepared for python will automatically pause for you after execution. Other editors allow you to configure the command line it uses to run your program. I find it particularly useful to configure it as "python -i myscript.py" when running. That drops you to a python shell after the end of the program, with the program environment loaded, so you may further play with the variables and call functions and methods.
cmd /k is the typical way to open any console application (not only Python) with a console window that will remain after the application closes. The easiest way I can think to do that, is to press Win+R, type cmd /k and then drag&drop the script you want to the Run dialog.
Start the script from an already open cmd window or
at the end of the script add something like this, in Python 2:
raw_input("Press enter to exit;")
Or, in Python 3:
input("Press enter to exit;")
To keep your window open in case of exception (yet, while printing the exception)
Python 2
if __name__ == '__main__':
try:
## your code, typically one function call
except Exception:
import sys
print sys.exc_info()[0]
import traceback
print traceback.format_exc()
print "Press Enter to continue ..."
raw_input()
To keep the window open in any case:
if __name__ == '__main__':
try:
## your code, typically one function call
except Exception:
import sys
print sys.exc_info()[0]
import traceback
print traceback.format_exc()
finally:
print "Press Enter to continue ..."
raw_input()
Python 3
For Python3 you'll have to use input() in place of raw_input(), and of course adapt the print statements.
if __name__ == '__main__':
try:
## your code, typically one function call
except BaseException:
import sys
print(sys.exc_info()[0])
import traceback
print(traceback.format_exc())
print("Press Enter to continue ...")
input()
To keep the window open in any case:
if __name__ == '__main__':
try:
## your code, typically one function call
except BaseException:
import sys
print(sys.exc_info()[0])
import traceback
print(traceback.format_exc())
finally:
print("Press Enter to continue ...")
input()
you can combine the answers before: (for Notepad++ User)
press F5 to run current script and type in command:
cmd /k python -i "$(FULL_CURRENT_PATH)"
in this way you stay in interactive mode after executing your Notepad++ python script and you are able to play around with your variables and so on :)
Create a Windows batch file with these 2 lines:
python your-program.py
pause
Using atexit, you can pause the program right when it exits. If an error/exception is the reason for the exit, it will pause after printing the stacktrace.
import atexit
# Python 2 should use `raw_input` instead of `input`
atexit.register(input, 'Press Enter to continue...')
In my program, I put the call to atexit.register in the except clause, so that it will only pause if something went wrong.
if __name__ == "__main__":
try:
something_that_may_fail()
except:
# Register the pause.
import atexit
atexit.register(input, 'Press Enter to continue...')
raise # Reraise the exception.
In python 2 you can do it with: raw_input()
>>print("Hello World!")
>>raw_input('Waiting a key...')
In python 3 you can do it with: input()
>>print("Hello world!")
>>input('Waiting a key...')
Also, you can do it with the time.sleep(time)
>>import time
>>print("The program will close in 5 seconds")
>>time.sleep(5)
On Python 3
input('Press Enter to Exit...')
Will do the trick.
You can just write
input()
at the end of your code
therefore when you run you script it will wait for you to enter something
{ENTER for example}
I had a similar problem. With Notepad++ I used to use the command : C:\Python27\python.exe "$(FULL_CURRENT_PATH)" which closed the cmd window immediately after the code terminated.
Now I am using cmd /k c:\Python27\python.exe "$(FULL_CURRENT_PATH)" which keeps the cmd window open.
To just keep the window open I agree with Anurag and this is what I did to keep my windows open for short little calculation type programs.
This would just show a cursor with no text:
raw_input()
This next example would give you a clear message that the program is done and not waiting on another input prompt within the program:
print('You have reached the end and the "raw_input()" function is keeping the window open')
raw_input()
Note!
(1) In python 3, there is no raw_input(), just
input().
(2) Use single quotes to indicate a string; otherwise if you type doubles around anything, such as
"raw_input()", it will think it is a function, variable, etc, and not text.
In this next example, I use double quotes and it won't work because it thinks there is a break in the quotes between "the" and "function" even though when you read it, your own mind can make perfect sense of it:
print("You have reached the end and the "input()" function is keeping the window open")
input()
Hopefully this helps others who might be starting out and still haven't figured out how the computer thinks yet. It can take a while. :o)
If you want to run your script from a desktop shortcut, right click your python file and select Send to|Desktop (create shortcut). Then right click the shortcut and select Properties. On the Shortcut tab select the Target: text box and add cmd /k in front of the path and click OK. The shortcut should now run your script without closing and you don't need the input('Hit enter to close')
Note, if you have more than one version of python on your machine, add the name of the required python executable between cmd /k and the scipt path like this:
cmd /k python3 "C:\Users\<yourname>\Documents\your_scipt.py"
Apart from input and raw_input, you could also use an infinite while loop, like this:
while True: pass (Python 2.5+/3) or while 1: pass (all versions of Python 2/3). This might use computing power, though.
You could also run the program from the command line. Type python into the command line (Mac OS X Terminal) and it should say Python 3.?.? (Your Python version) It it does not show your Python version, or says python: command not found, look into changing PATH values (enviromentl values, listed above)/type C:\(Python folder\python.exe. If that is successful, type python or C:\(Python installation)\python.exe and the full directory of your program.
A very belated answer, but I created a Windows Batch file called pythonbat.bat containing the following:
python.exe %1
#echo off
echo.
pause
and then specified pythonbat.bat as the default handler for .py files.
Now, when I double-click a .py file in File Explorer, it opens a new console window, runs the Python script and then pauses (remains open), until I press any key...
No changes required to any Python scripts.
I can still open a console window and specify python myscript.py if I want to...
(I just noticed #maurizio already posted this exact answer)
If you want to stay cmd-window open AND be in running-file directory this works at Windows 10:
cmd /k cd /d $(CURRENT_DIRECTORY) && python $(FULL_CURRENT_PATH)
I found the solution on my py3 enviroment at win10 is just run cmd or powershell as Administrator,and the output would stay at the same console window,any other type of user run python command would cause python to open a new console window.
The simplest way:
your_code()
while True:
pass
When you open the window it doesn't close until you close the prompt.
`import sys,traceback
sys.exc_info()[0]
traceback.format_exc()
print("Press Enter to exit ...")
input()`
simply write the above code after your actual code. for eg. am taking input from user and print on console hence my code will be look like this -->
`import sys,traceback
nam=input("enter your name:")
print("your name is:-{}".format(nam)) #here all my actual working is done
sys.exc_info()[0]
traceback.format_exc()
print("Press Enter to exit ...")
input()`
Try this,
import sys
stat='idlelib' in sys.modules
if stat==False:
input()
This will only stop console window, not the IDLE.
You can launch python with the -i option or set the environment variable PYTHONINSPECT=x. From the docs:
inspect interactively after running script; forces a prompt even
if stdin does not appear to be a terminal; also PYTHONINSPECT=x
So when your script crashes or finishes, you'll get a python prompt and your window will not close.
Create a function like dontClose() or something with a while loop:
import time
def dontClose():
n = 1
while n > 0:
n += 1
time.sleep(n)
then run the function after your code. for e.g.:
print("Hello, World!")
dontClose()
Go here and download and install Notepad++
Go here and download and install Python 2.7 not 3.
Start, Run Powershell. Enter the following. [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")
Close Powershell and reopen it.
Make a directory for your programs. mkdir scripts
Open that directory cd scripts
In Notepad++, in a new file type: print "hello world"
Save the file as hello.py
Go back to powershell and make sure you are in the right directory by typing dir. You should see your file hello.py there.
At the Powershell prompt type: python hello.py
On windows 10 insert at beggining this:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Strange, but it work for me!(Together with input() at the end, of course)
You can open PowerShell and type "python".
After Python has been imported, you can copy paste the source code from your favourite text-editor to run the code.
The window won't close.
A simple hack to keep the window open:
counter = 0
While (True):
If (counter == 0):
# Code goes here
counter += 1
The counter is so the code won’t repeat itself.
The simplest way:
import time
#Your code here
time.sleep(60)
#end of code (and console shut down)
this will leave the code up for 1 minute then close it.