How to allow c printf to print in ipython notebook in cython cell?

一世执手 提交于 2020-08-07 05:50:43

问题


%%cython
from libc.stdio cimport printf
def test():
    printf('abc')

If I run test(), it doesn't print anything.

Currently I am doing something stupid as:

cdef char s[80]
sprintf(s, 'something')
print s

What's a better way to use printf in cython? Why doesn't it print?


回答1:


You can use the wurlitzer package to capture C-level stdout / stderr and redirect it to IPython.

For example, include the following code blocks in your Jupyter notebook:

%load_ext Cython
%load_ext wurlitzer
%%cython
from libc.stdio cimport printf
def test():
    printf('abc')
test()
# prints "abc"



回答2:


Output will be printed in your terminal (console) running Jupyter notebook. not sure how to display it in Jupyter though.



来源:https://stackoverflow.com/questions/29262667/how-to-allow-c-printf-to-print-in-ipython-notebook-in-cython-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!