Print statements in pyjulia


#1

In pyjulia, how do I get “print” statements to show up in the Python terminal? For example, this doesn’t print anything

>>> Main.eval(“print(123)”)

My real use case is to get the results from @benchmark

>>> Main.eval("""
using BenchmarkTools
@benchmark x = 123""")


#2

See the discussion in https://github.com/JuliaPy/pyjulia/issues/49


#3

Are you using it in standard python REPL? I think print etc. in Julia should work without any tricks. (Using PyJulia in Jupyter Notebook/Lab needs more improvements, though.)

What is your output when you evaluated the following?

>>> from julia import Main
>>> Main.eval("stdout")
<julia.core.PyIO object at 0x7f46f0075518>
>>> import sys
>>> sys.stdout
<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>

(In case you are using Julia 0.6, it’s Main.eval("STDOUT").)


#4

I was originally testing code on a Jupyter notebook. When I run Main.eval(“print(123)”) in the standard python REPL, the output does show up! However, even in the REPL, I still don’t see the results of Main.eval("""@benchmark x = 123""")

On the code you asked me to evaluate:

#####################################
# Evaluated on standard Python REPL #
#####################################
>>> from julia import Main
>>> Main.eval("stdout")
<julia.core.PyIO object at 0x7f46f0075518>
>>> import sys
>>> sys.stdout
<_io.TextIOWrapper name='<;stdout>' mode='w' encoding='UTF-8'>;
#################################
# Evaluated on Jupyter notebook #
#################################
from julia import Main
Main.eval("stdout")
<julia.core.PyIO at 0x7f0bf46a12b0>

import sys
sys.stdout
<ipykernel.iostream.OutStream at 0x7f0bf800dcc0>

I’m using Python 3.7.0, IPython 7.2.0, Julia 1.0.2, and the latest github master version of pyjulia (0.2.1.dev0)


#5

I guess you saw <PyCall.jlwrap Trial(0.024 ns)> as below? This is because Julia REPL runs display on the returned value of the expression you type. In python REPL, you need to run it manually, like this:

>>> from julia import Main
>>> Main.eval("using BenchmarkTools")
>>> Main.eval("@benchmark 1")
<PyCall.jlwrap Trial(0.024 ns)>
>>> Main.display(_)
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     0.024 ns (0.00% GC)
  median time:      0.029 ns (0.00% GC)
  mean time:        0.030 ns (0.00% GC)
  maximum time:     13.283 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1000>>>
>>>
>>> Main.eval("display(@benchmark 1); println()")
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     0.026 ns (0.00% GC)
  median time:      0.030 ns (0.00% GC)
  mean time:        0.031 ns (0.00% GC)
  maximum time:     0.197 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1000
>>>

I know this needs improvement. Python does not have a systematic display system like Julia so it’s a bit hard. But we can do it in IPython as it has an excellent display system. Mapping Julia’s display system to IPython’s will be done in https://github.com/JuliaPy/PyCall.jl/issues/617. For I/O support in Jupyter, see https://github.com/JuliaPy/pyjulia/issues/49 as @stevengj suggested.


#6

Ok, thank you for the explanation!