pyo3: Rust print can't displayed in notebook environment

I tried the official example- https://pyo3.rs/v0.16.2/trait_bounds.html. It works well when the code is in .py file, I can see the print from python and rust. But when I use it in jupyter notebook(vscode), the rust print can not display.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

I think we might be able to have py.stdout() and py.stderr() functions to give structures which use PySys_WriteStdout and PySys_WriteStderr ? Just need someone who’s got time to play with an implementation.

I gave this a try and got it to work. See here.

It’s unclear to me if my method is thread safe though. Is it necessary to acquire the GIL when calling pyo3 ffi functions, specifically PyObject_CallMethod, PySys_GetObject, PySys_WriteStderr, PySys_WriteStdout? The first two of those are used only to call sys.stdout.flush().

I think #1208 tracks integration between Rust logging and Python logging. The guide also used to mention https://github.com/vorner/pyo3-log and there is also tracing integration project https://github.com/danielschemmel/tracing-for-pyo3-logging