Best way to run Julia code in an IPython notebook (or Python code in an IJulia notebook)

Question:

My goal is to run only a few lines of Julia in an IPython notebook where the majority of the code will be Python for some experiments …

I found a nice example notebook here:

http://nbviewer.ipython.org/github/JuliaLang/IJulia.jl/blob/master/python/doc/JuliaMagic.ipynb

And now I am wondering how I would install the IPython extension for Julia (I am primarily using IPython 2.1) so that I can load it via

%load_ext julia.magic

I am also very new to julia and I am wondering if there is a performance benefit of “mixing numpy and julia” as shown in this notebook (over regular Python numpy or regular Julia code)

When I understand the concept correctly, I would use IJulia notebooks (which I set up successfully) if I am only interested in running Julia code?

I installed IJulia, and i can also run IJulia notebooks, but I actually only wanted to have a small portion of Julia code in my notebook, the rest should be Python / Cython.
Unfortunately, I read that magic functions are not yet fully supported: “One difference from IPython is that the IJulia kernel currently does not support “magics”, which are special commands prefixed with % or %% to execute code in a different language”

Is there a way to run Python code in IJulia notebooks?

Asked By: user2489252

||

Answers:

Run Julia inside an IPython notebook

Hack

In order to run Julia snippets (or other language) inside an IPython notebook, I just append the string 'julia' to the default list in the _script_magics_default method from the ScriptMagics class in:

  • /usr/lib/python3.4/site-packages/IPython/core/magics/script.py or
  • /usr/lib/python2.7/site-packages/IPython/core/magics/script.py.

Example:

# like this:
defaults = [
    'sh',
    'bash',
    'perl',
    'ruby',
    'python',
    'python2',
    'python3',
    'pypy',
    'julia', # add your own magic
]

IPython *Julia magic*

Julia Magic (Bi-directional)

To use %load_ext julia.magic, you would need to run the setup.py here:

Update (09/04/2014): the setup.py file has been moved to pyjulia.jl:


Which you get when Pkg.add("IJulia") clones the repo in your filesystem:

cd ~/.julia/v0.3/IJulia/python/
sudo python2 setup.py install

Currently this only works for me in Python2. Python3 complains about:

ImportError: No module named 'core'

when I try to load the extention, but installs without complains.

After installing it you can also do this from inside Python2:

from julia import Julia
j = Julia()
arr = j.run('[1:10]')
type(arr) # numpy.ndarray

Runing a script from your system shell

Use the shell mode syntax in a notebook cell:

!julia my_script.jl


Run Python inside IJulia notebook

Using PyCall

It’s not really running python code in the context you want, but you can also use Python libraries from within Julia:

using PyCall
@pyimport math
println(math.pi)

Runing a script from your system shell

Use the shell mode syntax in a notebook cell:

;python my_script.py
Answered By: HarmonicaMuse

Another option is to use Beaker. They have a tutorial notebook available that mixes R and Python; using Julia is just as easy.

Answered By: Jeff Hammerbacher

If you want to run a whole notebook of only julia (or where you call other languages only from julia), then there is a cleaner solution. First, launch julia and do

Pkg.add("IJulia")

to get the IJulia package. Then you can

ipython notebook --profile julia

and your notebook will have julia as the native (default) language.

h/t to David Sanders and his excellent julia tutorials that are written in IPython notebooks; video here.

Answered By: David Ketcheson

To complete this good answer, Without any hack, and without modifying any system file, a simple solution is with the %%script magic:

In [1]: %%script julia
   ...: println("Hi from a Julia sub-process")
   ...: a = [1:10]

Be cautious that the cell was run in a sub-process, so whatever was done in it is not accessible by the rest of the IPython session:

In [2]: print("Hi from the main IPython process")
   ...: print(a)  # <-- not available from the Julia code, will fail !
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-c5a4f3535135> in <module>()
----> 1 print(a)

NameError: name 'a' is not defined
Answered By: Næreen

A clean and nice method is described in this notebook: https://github.com/binder-examples/julia-python/blob/master/python-and-julia.ipynb.

It strongly uses IPython magic (%julia <julia code> inlined in Python, and %%julia cells), but the result is quite impressive: the two Python and Julia processes can use the same variables and memory.

Answered By: Næreen
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.