Issue
When I run Python code in the jupyter notebook, I got the following error, and cell doesn't finalize.
However, if I run the same Python code in the command line, it finishes its run without any problem.
[IPKernelApp] ERROR | Exception in message handler:
Traceback (most recent call last):
File "/Users/burcakotlu/developer/python/test_venv/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 424, in dispatch_shell
await result
File "/Users/burcakotlu/developer/python/test_venv/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 770, in execute_request
sys.stderr.flush()
ValueError: I/O operation on closed file.
[IPKernelApp] ERROR | Error in message handler
Traceback (most recent call last):
File "/Users/burcakotlu/developer/python/test_venv/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 529, in dispatch_queue
await self.process_one()
File "/Users/burcakotlu/developer/python/test_venv/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 518, in process_one
await dispatch(*args)
File "/Users/burcakotlu/developer/python/test_venv/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 437, in dispatch_shell
sys.stderr.flush()
ValueError: I/O operation on closed file.
I haven't solved this issue for days. I would appreciate any help.
Env specific versions:
$ pip list | grep -E "(^jup|^ipy)"
ipykernel 6.27.1
ipython 8.18.1
jupyter_client 8.6.0
jupyter_core 5.5.0
Solution
Thanks for your help. Comments from @julaine writing that the server can't flush stderr, which is very unusual wake me up.
I remembered that somewhere in my code I have done the following.
sys.stderr = open(error_file, 'w')
...
...
...
sys.stderr.close()
So I had redirected the sys.stderr to a file and then closed it. Then I got the following error
sys.stderr.flush()
ValueError: I/O operation on closed file.
Changing the code as follows solved the issue:
temp = sys.stderr
sys.stderr = open(error_file, 'w')
...
...
...
sys.stderr.close()
sys.stderr = temp
Redirecting sys.stderr to what it was pointing before resolved the problem.
There is even a better solution without using a temporary variable.
sys.stderr = open(error_file, 'w')
...
...
...
sys.stderr.close()
sys.stderr = sys.__stderr__
Answered By - burcak
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.