I have a very simple Python program.
def square(a):
for i in a:
yield i*i
def main():
a = [1, 2, 3]
b = square(a)
print(b)
print(next(b))
print(next(b))
print(next(b))
print(next(b))
if __name__ == "__main__":
main()
When I run it from the command line, I got the following result, which is what I was expecting.
c:\test1>python main.py
<generator object square at 0x000002982E07A4A0>
1
4
9
Traceback (most recent call last):
File "main.py", line 16, in <module>
main()
File "main.py", line 12, in main
print(next(b))
StopIteration
c:\test1>
I run it many times and the result is always the same. However, when I use IntelliJ IDEA with Python plugin to run the same program, I got this:
C:\Python\python.exe c:/test1/main.py
Traceback (most recent call last):
File "c:/my_projects/python1/main.py", line 16, in <module>
main()
File "c:/my_projects/python1/main.py", line 12, in main
print(next(b))
StopIteration
<generator object square at 0x000001799923C580>
1
4
9
Process finished with exit code 1
Or this:
C:\Python\python.exe c:/test1/main.py
<generator object square at 0x00000230F551C580>
1
4
9
Traceback (most recent call last):
File "c:/my_projects/python1/main.py", line 16, in <module>
main()
File "c:/my_projects/python1/main.py", line 12, in main
print(next(b))
StopIteration
Process finished with exit code 1
Why does this happen? How to get a stable result in IntelliJ IDEA?
The Kemp's explanation is correct.
There is a corresponding ticket in PyCharm issue tracker https://youtrack.jetbrains.com/issue/PY-37396
See also https://youtrack.jetbrains.com/issue/PY-32776
As a possible workaround, you can enable Emulate terminal in output console option for your run configuration (Run | Edit Configurations...).
The issue here is that the print output goes to stdout while the exception message goes to stderr. In your terminal they get output as-is, but the IDE is likely doing some buffering on stdout for efficiency reasons. The same buffer won't apply to stderr because it needs to make sure that output gets on the screen asap and without risking the buffer being prematurely cleared out. This means that sometimes (quite often in my experience) exception messages like yours will appear before normal output that you'd expect to already have been shown.
Related
I have a shell script, which runs a python file and does some log. I first redirect all stdout and stderr to a specific file. but noticed that, a print statement in python does not show their print result to my specific log file.
This is my shell file:
date=$(date +"%D")
exec 3>&1
exec 4>&2
exec 1>>/root/shell/auto.log
exec 2>>/root/shell/auto.log
header=$(printf "%-10s" "=")
echo "${header}${date}--start${header}"| sed "s/ /=/g"
python /root/shell/someAuto.py
echo "${header}${date}--end${header}"| sed "s/ /=/g"
exec 1>&3
exec 2>&4
I add some demo case to redo this problem.The simple python file can be that:
#!/usr/bin/python3
import time
import sys
for i in range(10):
print(i)
time.sleep(5)
sys.stdout.flush()
when start run shell script, tail -f auto.log, start headers print quickly and success, but wait longer than every 5 secs, no print result show, i don't know why..
here some log.The reason why I always KeyboardInterrupt is that it run so long, it's not normal, so I stop it.
==========06/16/20--start==========
0
1
Traceback (most recent call last):
File "/root/shell/testLog.py", line 6, in <module>
time.sleep(5)
KeyboardInterrupt
==========06/16/20--end==========
==========06/16/20--start==========
0
Traceback (most recent call last):
File "/root/shell/testLog.py", line 6, in <module>
time.sleep(5)
KeyboardInterrupt
==========06/16/20--end==========
==========06/16/20--start==========
0
Traceback (most recent call last):
File "/root/shell/testLog.py", line 6, in <module>
time.sleep(5)
KeyboardInterrupt
==========06/16/20--end==========
When #A runs
def add_particle_to_atom(particle_uid, atom_uid):
node_data = find_node_by_uid(particle_uid) #A
particle = node_data['queryset'] #B
It triggers this function that I thought would cause an exception in the REPL.
def find_node_by_uid(node_uid):
# ...
try:
# thing will fail because i'm giving it garbage for a uid
except:
print("|--- ERROR: there is no particle with that uid.")
return HttpResponseNotFound("404")
But the console give an error that is associated with #B.
>>> add_particle_to_atom('wefjhwljefh',a)
|--- CHECK: if particle with uid 'wefjhwljefh' exists.
|--- ERROR: there is no particle with that uid.
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/layne/Desktop/AlchDB/alchemy/views.py", line 258, in add_particle_to_atom
particle = node_data['queryset']
File "/Users/layne/Desktop/venv_alchdb/alchdb/lib/python3.7/site-packages/django/http/response.py", line 145, in __getitem__
return self._headers[header.lower()][1]
KeyError: 'queryset'
Why does it do this when I have told the sub-function to 404? And I know it is failing because the print is running. I don't want to exit because I still want the REPL usable.
UPDATE: If I put #A's return before the print, it skips the print and goes straight back to executing the main function.
On my iMac, the debugger, running a faulty python script (on MAC OS Sierra), always points to the first active line of code as cause of an exception, while when launching without debugger the correct line is identified. Does anybody have an idea why this may occur and how to fix it?
Here's a simple example with a "File not found" exception:
The script exception_test.py:
1 # Some dummy lines...
2 a=1
3 b=2
4 c=a+b
5 # Lines casuing the exception:
6 with open("filename","r") as fid:
7 lines=fid.readlines()
When run without debugger, as in python exception_test.py it yields
Traceback (most recent call last):
File "exception_test.py", line 6, in <module>
with open("filename","r") as fid:
IOError: [Errno 2] No such file or directory: 'filename'
identifying the correct line, i.e. line6,
while python -m pdb exception_test.py and successive c to continue yields
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pdb.py", line 1314, in main
pdb._runscript(mainpyfile)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pdb.py", line 1233, in _runscript
self.run(statement)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/bdb.py", line 400, in run
exec cmd in globals, locals
File "<string>", line 1, in <module>
File "exception_test.py", line 2, in <module>
a=1
IOError: [Errno 2] No such file or directory: 'filename'
Uncaught exception. Entering post mortem debugging
indicating the first active line of code, i.e. line 2.
It is more likely an issue of cpython and pdb, not your code, pypy could print correct traceback line number.
the line_no of the frame where the exception occurred is not right under -m pdb.
after some guessing and tracing, I could narrow to this path:
sys.settrace(self.trace_dispatch)
-> trace_dispatch() -> dispatch_line() -> user_line() -> interaction()
the real root cause is still not clear.
I have a .py file that has a number of functions. Now I am debugging the codes and find that sometimes the program is stuck somewhere.
If I just leave it there and wait for a super long time, error message shows up:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 9, in generate
File "<stdin>", line 7, in choosePath
MemoryError
I have no idea where it is stuck, as I have several while and for loops. Is there any easy way to figure out that easily? I feel really reluctant to debug one loop by another.
Hit CTRL-C and look at the traceback.
For example, the following code will hit a never-ending loop:
import time
def foo():
while True:
time.sleep(1)
def bar():
for i in range(10):
foo()
bar()
and when I interrupt it I see:
$ bin/python endless_demo.py
^CTraceback (most recent call last):
File "test.py", line 11, in <module>
bar()
File "test.py", line 9, in bar
foo()
File "test.py", line 5, in foo
time.sleep(1)
KeyboardInterrupt
The traceback ends in foo, on line 5. This is where Python was busy when I interrupted the program. The traceback also tells me that first bar() was called, which called foo(), so I can see how we got there.
Note that if you have a bare except handler this won't necessarily work; catching all exceptions with try: except: catches KeyboardInterrupt too. Always use except Exception: at a minimum to prevent catching system exceptions.
def main():
try:
print "hardfart"
return 0
except:
return 1
if __name__ == '__main__':
exit(main())
Can one kind programmer tell me why this spits out the following error on exit?
Traceback (most recent call last):
File "C:/Apps/exp_exit.py", line 9, in ,module.
exit(main())
File "C:\Apps\python2.7.2\lib\site.py", line 372 in __call__
raise SystemExit(code)
SystemExit: 0
This is causing an error on exit in a project that's set up similarly. For that project, after using gui2exe to compile an exe, when closing the program I get this related error:
Traceback (most recent call last):
File "checkHDBox.py", line 303, in <module>
NameError: name 'exit' is not defined
So if exit is generating this error, how do I exit then? And if I create an exception handler for exit, doesn't that replace the default action that python takes with the exit function?
Thanks.
Edit:
I think this answers my own question.
The traceback here is from IDLE, I think it's a default behavior from other sources I've read.
Traceback (most recent call last):
File "C:/Apps/exp_exit.py", line 9, in ,module.
exit(main())
File "C:\Apps\python2.7.2\lib\site.py", line 372 in __call__
raise SystemExit(code)
SystemExit: 0
The traceback here was fixed by using sys.exit() instead of exit(0)
Traceback (most recent call last):
File "checkHDBox.py", line 303, in <module>
NameError: name 'exit' is not defined
You exit a program by raising SystemExit. This is what exit() does. Someone has incorrectly written an exception handler that catches all exceptions. This is why you only catch the exceptions you can handle.