Search code examples
pythonpython-click

Click not working in jupyter


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


Solution

  • 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