I have a found a Julia function that nicely does the job I need. How can I quickly integrate it to be able to call it from Python?
Suppose the function is
f(x,y) = 2x.+y
What is the best and most elegent way to use it from Python?
I have a found a Julia function that nicely does the job I need. How can I quickly integrate it to be able to call it from Python?
Suppose the function is
f(x,y) = 2x.+y
What is the best and most elegent way to use it from Python?
I found that the python package PyCall
did not work for me (segaults in unexplicable ways) however the more recent library JuliaCall worked. The differences are listed in the above link to the official pip-package. There is also an example on how to get started but I will describe the process here as well.
juliacall
The advantages of using juliacall over pycall are
PythonCall
into Julia either by entering package mode via ]
and writing add PythonCall
or execute in normal julia mode:julia> using Pkg
julia> Pkg.add("PythonCall")
juliacall
into you python environment:$ python -m pip install juliacall
;
and execute:shell> which julia
/home/user/.juliaup/bin/julia
Pkg
and then entering shell mode again to execute:julia> using Pkg
shell> pwd $(Pkg.envdir())
/home/user/.julia/environments
shell> ls -lah $(Pkg.envdir())
v1.6
So my julia-env path is: /home/user/.julia/environments/v1.6/
import os
# setting env variables so that juliacall finds the correct julia env
# see the github page for docs about env variables https://github.com/cjdoris/PyJuliaPkg
# juliacall makes use of the package pyjuliapkg to find the correct julia version, so you can read up on that here: https://github.com/cjdoris/pyjuliapkg/blob/main/src/juliapkg/find_julia.py
os.environ['PYTHON_JULIAPKG_EXE'] = "/home/user/.juliaup/bin/julia"
os.environ['PYTHON_JULIAPKG_OFFLINE']= 'yes' # such that nothing new is downloaded
os.environ['PYTHON_JULIAPKG_PROJECT'] = '/home/user/.julia/environments/v1.6/'
from juliacall import Main as jl
def main():
print('in main...')
# show installed julia packages to make sure you are in the right env
jl.seval('using Pkg')
jl.seval('Pkg.status()')
# import your script and execute
jl.seval('include("test_julia_simple.jl")')
result = jl.seval('f([2,3], [4,5])')
print(result)
print('...done')
return
if __name__ == "__main__":
main()
The accompanying julia script test_julia_simple.jl
contains your function and in this case looks as follows:
function f(x,y)
return 2x.+y
end
Assuming your Python and Julia are installed you need to take the following steps.
Run Julia and install
PyCall
Put your code into a Julia package
In the folder
src
you will findMyPackage.jl
, edit it to look like this:Install pyjulia
(On Linux systems you might want to use
python3
instead ofpython
command)For this step note that while an external Python can be used with Julia. However, for a convenience it might be worth to consider using a Python that got installed together with Julia as
PyCall
. In that case for installation use a command such this one:or on Linux
Note that if you have
JULIA_DEPOT_PATH
variable defined you can replace%HOMEPATH%\.julia
or~/.julia/
with its value.Run the appropiate Python and tell it to configure the Python-Julia integration:
Now you are ready to call your Julia code:
It is worth noting that the proposed approach in this answer has several advantages over a standalone Julia file which would be possible, although is less recommended. The advantages include:
EDIT
In February 2022
juliacall
was announced, as of December 2022juliacall
might be an easier option for some users - have a look at: How to load a custom Julia package in Python using Python's juliacall