In a directory I have three files.
test.py, containing print "Hello World"
python_runner.bat, containing
python test.py
PAUSE
jython_runner.bat, containing
jython test.py
PAUSE
The python_runner.bat works as expected, but running Jython_runner.bat causes the PAUSE command to be skipped!
Why is Jython causing the batch script to be prematurely terminated?
(NOTE: I am using Jython2.7b4, I haven't tried with Jython 2.5)
If the jython command is a batch script then the pause and any thing after this will not be executed.
try call
call jython test.py
What is the outcome?
Related
I am trying to set up a windows scheduled task to run a python script that runs two other scripts. I use os package to do it with the code like below:
#mainScript.py
import os
os.system('script1.py 1')
os.system('scripts.py 1')
The script works in a scheduled task except when I add the above code. What I mean If use script1.py in my bat file it works, but if I use the mainScript.py it gives me the error code.
'script1.py' is not recognized as an internal or external command,
operable program or batch file.
'script2.py' is not recognized as an internal or external command,
operable program or batch file.
I know I can just move all the code into mainScript but wonder if there is anything I can do to have.
You need to specify wich program will run the script.
Try this :
os.system('python script1.py')
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 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.
I have a python script containing a unittest.TestCase, with a setUp() function and a small number of test_foo_does_bar()-type functions.
The script ends as follows:
if __name__ == '__main__':
unittest.main()
When I run this script in Spyder (see below for config details) the interpreter stops at the following line:
----------------------------------------------------------------------
Ran x tests in x.xxxs
FAILED (failures=x)
No amount of Ctrl+C or Ctrl+D can rescue the interpreter and get me back to the prompt. The same script, run from the command prompt, terminates as normal.
Is this a bug in Spyder or am I missing something?
Setup info:
Spyder 2.2.3
Python 2.7.5 64bits. Qt 4.8.4, PyQt4 (API v2) 4.9.6 on Windows
It doesn't crash the Python interpreter, it terminates it. This is normal behavior for the script.
You can see it yourself: in main.py it is stated, main = TestProgram; this means that unittest.main() will call the TestProgram class, which in its __init__(self) calls self.runTests(), which ends with this:
if self.exit:
sys.exit(not self.result.wasSuccessful())
So it has a specific call to sys.exit(), which terminates the interpreter.
In fact, if you run it as command line, you return to the command line; and if you start command-line Python, import the module and call the function, you will see that you'll return to the command prompt - you don't stay in Python.
If you want the script to NOT terminate the interpreter, just state it when calling the funcion:
unittest.main(exit=False)
I have a python script that uses subprocess:
import subprocess
print "Running stuff"
subprocess.check_call(["do_stuff.bat"])
print "Stuff run"
If this was named blah.py, and I run (from a command prompt):
python blah.py
I will get the output from do_stuff.bat (or whatever I run).
If this is run as:
blah.py
Then I do not get output from do_stuff.bat, only the print statements.
So far seen on windows Server 2003. Python version 2.5.2 (stuck there for various reasons).
Looking at the associated file type action I see:
Python.File="C:\Python25\python.exe" "%1" %*
So can anyone explain the difference?
I had common problem using threads, but all of my code was in python. Threads can not write to standard output using print. Just main thread could do that. I used somethnig like this
import sys
sys.stdout.write("this was printed by thread")
I know that probably it wont help you with bat file...