JuliaPy / PyCall.jl

Package to call Python functions from the Julia language
MIT License
1.47k stars 187 forks source link

print function in python not works called by julia through PyCall #912

Closed ywlds closed 2 years ago

ywlds commented 3 years ago

The print function inside a python function when called by a julia function through PyCall seems not "print" anything at all? How can I fixed it, so that when used Julia and Python, we can debug more interactively?

stevengj commented 3 years ago

Works for me:

julia> PyCall.py"print(\"Hello world\")"
Hello world

Are you running in a different environment, like a Jupyter notebook? You may have a problem from the fact that Python's stdio streams are not the same as Julia's, so they might not be captured by the notebook.

marcpabst commented 2 years ago

I can confirm that this does not work in a Jupyter notebook. Any workarounds?

stevengj commented 2 years ago

It's just buffering. pyimport("sys").stdout.flush() causes the output to appear in Jupyter.

stevengj commented 2 years ago

We can do this automatically with

if isdefined(Main, :IJulia) && IJulia.inited
    Main.IJulia.push_postexecute_hook(() -> pyimport("sys")."stdout"."flush"())
end

Maybe we should do this in PyCall's __init__ function?

marcpabst commented 2 years ago

Great, thanks!