spyder-ide / spyder

Official repository for Spyder - The Scientific Python Development Environment
https://www.spyder-ide.org
MIT License
8.38k stars 1.62k forks source link

Py_Initialize: can't initialize sys standard streams error when creating a kernel #4672

Closed sebma closed 7 years ago

sebma commented 7 years ago

Description of your problem

IPython console fails to start

What steps will reproduce the problem?

  1. Launch Spyder 3.1.3 (same pb. with 3.1.4)
  2. IPython console fails to start

**What is the expected output?

IPython console start

What do you see instead?**

An error ocurred while starting the kernel
Fatal Python error: Py_Initialize: can't initialize sys standard streams
OSError: [Errno 9] Bad file descriptor

Current thread 0x00007fffc12213c0 (most recent call first):
Fatal Python error: Py_Initialize: can't initialize sys standard streams
OSError: [Errno 9] Bad file descriptor

Please provide any additional information below

Versions and main components

Dependencies

jedi =0.9.0 : 0.9.0 (OK) matplotlib >=1.0 : 2.0.2 (OK) nbconvert >=4.0 : 5.2.1 (OK) numpy >=1.7 : 1.13.0 (OK) pandas >=0.13.1 : 0.20.2 (OK) pep8 >=0.6 : 1.7.0 (OK) psutil >=0.3 : 5.2.2 (OK) pyflakes >=0.6.0 : 1.5.0 (OK) pygments >=2.0 : 2.2.0 (OK) pylint >=0.25 : 1.6.4 (OK) qtconsole >=4.2.0: 4.3.0 (OK) rope >=0.9.4 : 0.9.4-1 (OK) sphinx >=0.6.6 : 1.6.2 (OK) sympy >=0.7.3 : None (NOK)

ccordoba12 commented 7 years ago

Sorry, I have no idea why this is happening.

@minrk or @takluyver, have you seen this error before?

takluyver commented 7 years ago

Does spyder do anything to redirect standard streams? I wonder if the kernel is trying to inherit its standard streams from the parent process, and picking up something that's not a valid fd.

sebma commented 7 years ago

Can you please fix this issue before releasing Spyder 3.2 because I do need the IPython console for debugging with Spyder ?

sebma commented 7 years ago

@takluyver I think I understand a little bit of your question. If I run Spyder with this command :

spyder &
exit

the IPython console cannot start, but if I run it with this command spyder & without existing the calling shell before Spyder+IPython finishes to start, it seems to work.

Hope this helps you fixing this.

ccordoba12 commented 7 years ago

Does spyder do anything to redirect standard streams?

Yes, we redirect the kernel stderr output to a file, as can be seen here

https://github.com/spyder-ide/spyder/blob/master/spyder/plugins/ipythonconsole.py#L1293

but we haven't seen other errors as the one reported here, so I don't know why this is happening.

ccordoba12 commented 7 years ago

@sebma, I can't reproduce this on Linux and I don't even know how to address this, sorry.

But at least you have a temporary solution.

takluyver commented 7 years ago

Where do you run those lines? Are you typing them into a shell, or using them in some sort of script?

sebma commented 7 years ago

@takluyver On macos, I'm typing them into a shell

takluyver commented 7 years ago

OK, so you close the shell, which presumably results in the terminal it's running in being cleaned up. So the standard stream fds Spyder is using are invalidated. I can kind of reproduce something similar with the Qtconsole on Linux - calling sys.__stderr__.flush() after exiting the terminal I launched it from gives me [Errno 5] Input/output error.

It's up to @ccordoba12 , but I'd be inclined to compare the situation to this old joke:

Doctor, it hurts when I do this!

Don't do that.

ccordoba12 commented 7 years ago

@takluyver, yep, that's my feeling about this situation ;-)

@sebma, I don't know how we could fix this because if you abruptly remove the carpet under which Spyder is running, then things are expected to fail.

@takluyver, thanks a lot for chiming in!

sebma commented 7 years ago

I don't want any spyder on my carpet ! Let them do what they were created for : catching mosquitoes from their web and eating them. LOL