When calling printf by ctypes, jupyter does not show output from printf

the code:

from ctypes import *

libc = cdll.LoadLibrary('msvcrt.dll')
libc.printf(c_char_p(b'Hello %d %.2f\n'), c_int(16), c_double(2.3))

output from standart python:

Hello 16 2.30

output from ipython:

Hello 16 2.30
Out[31]: 14

output from jupyter:

Out[15]: 14

How to make jupyter display “Hello 16 2.30”?

I think @minrk has you covered. See here about ‘wurlitzer’ that looks designed to address this. I just tested in the classic notebook interface and it seems to work although the order is reversed from your first listing. (For ubuntu users, replace the ctype import & libc assignment with import ctypes; libc = ctypes.CDLL(None) based on Min’s post.)

Above the code you wish run, make a new cell in your notebook with the folllowing and run it first and then try running your code:

%pip install wurlitzer
%load_ext wurlitzer

I also noted there may be some related solutions here but ‘wurlitzer’ seems easiest.


Thank you very much. It’s very helpful.

1 Like