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)
Related
I am running a script into ipython (1.2.1) and I need it to stop if a certain condition is not met. I tried to use the exit() statement, but it is not behaving as expected.
Take for example the following script which I called test.py:
if(True):
print('Error')
exit()
print('Still here!')
When I run it using python test.py, I get:
$python test.py
Error
And then the execution is terminated, as expected.
But if I run it from ipython using run -i test.py, then I get:
In [1]: run -i test.py
Error
Still here!
And finally the execution of ipython is terminated. The problem is that in this case the second print statement is still executed, while I would need the execution of the script to be terminated as soon as the exit() statement is encountered.
Why is this happening and how can I obtain the result I want? (I am running python 2.7.6)
exit() alone is meant for the REPL, not really meant to be used by scripts.
Try using sys.exit(0) after you import sys, of course
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?
I am running an application that has an embedded python interpreter which is 2.7. I need to run a standalone python application as a subprocess, but that application is only compatible with python 2.6. How can I force the python application to launch with the python2.6 interpreter?
Like Dunes already said:
proc = Popen(['/path/to/executable', '-flag1', '--opt=value'])
The executable could be Python script with the shebang #!/usr/bin/env python2.6 or a bash script starting your Python2.6 interpreter.
Note, that the interpreter will not wait for the 2.6 version to finish. You have to use the .wait() method for that. Otherwise your child process can become a zombie.
# wait for the process to finish
proc.wait()
If you want to create a daemon - meaning a child process that does not terminates if the parent is killed - you need the os.fork() function. There are scripts around that takes care of all the stuff for you - eg. daemonize.py
A few complications, the python script is not a .py script, but a wrapper bash executable (with #! /usr/bin/env python2).
To run the script using python2.6, you could change its shebang to point to python2.6 executable:
#!/usr/bin/env python2.6
and run the script directly:
subprocess.check_call(['/path/to/your_script'] + sys.argv[1:])
sys.argv[1:] is used to pass command-line arguments to the child script.
If you can't change the shebang then run:
subprocess.check_call(['/usr/bin/env', 'python2.6', '/path/to/your_script'] +
sys.argv[1:])
I'm having a curious problem. I have a bash script that is calling a python script within it. The python script executes successfully, but never fully terminates
Content of Bash script:
#! /usr/bin/env bash
python python_script.py
echo "bar"
content of Python script:
#Much stuff
sys.exit("The python script just ended")
What I expect to see on termination would be:
>The python script just ended
>bar
What I instead get is:
>The python script just ended
If I keyboard interrupt, the bash continues as:
^C>bar
What gives? Clearly the exit is calling properly, and there is nothing between that and the output statement in the bash script that called the python script.
(I can't necessarily give specifics on the workings of the "Much stuff" in the python script, as I'm modifying existing code that I don't fully understand. I've mostly left the workings of the script alone, and modified output more than anything for formatting, but I'm happy to try and provide you with any additional information requested)
What sys.exit() does is throw an exception of type SystemExit. If your script were to catch this exception, it would continue executing past sys.exit().
Also, if you have non-daemon threads, these could be preventing the process from terminating.
If that's the case, you can either turn them into daemon threads, or somehow signal to them that you wish to exit the script, and let them shut themselves down.
Finally, there's os._exit(), but you should not have to resort to that.
The following also works:
import os
print('Hellow world')
os._exit(os.EX_OK)
In Ubuntu, it exits the terminal. See also the example in this link Program With the os.exit() Method
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...