Reputation: 6429
just found the following . running via notebook
import
.echo("test")
Output:
/home/user/anaconda3/envs/p36/lib/python3.6/site-packages//utils.py in echo(message, file, nl, err, color)
257
258 if message:
--> 259 file.write(message)
260 file.flush()
261
UnsupportedOperation: not writable
Has someone seen this before and knows how to work around? I have to use a lib via that uses . so not is not possible.
Update: This commit to a jupyter branch of click solves the issue: https://github.com/elgalu/click/commit/1cb7aaba8c9dd6ec760d3e7e414d0b4e5f788543#diff-d17772ee4f65879b69a53dbc4b3d42bd
Upvotes: 2
Views: 1217
Reputation: 2826
In my case using Python 3 I wanted to preserve the click styling on my message both in the Jupyter notebook and when the code was run in the terminal. I handled it this way:
from io import UnsupportedOperation
import click
item = 'Your Name'
message = click.style('"{}"'.format(item), fg='red', bold=True)
try:
click.echo(message)
except UnsupportedOperation as err:
print('Error: "{}"'.format(err))
print(message)
The color is preserved in the notebook:
Upvotes: 1
Reputation: 25769
I think that Jupyter hijacks and locks the STDOUT
/STDERR
(at least the one click
is trying to use) and if you don't provide a stream to click.echo()
it will attempt writing to the STDOUT
/STDERR
, hence the error.
You can work around it by passing an output stream like STDOUT
yourself:
import click
import sys
click.echo("test", sys.stdout)
# test
Upvotes: 4