I can not call Julia from Python

Viewed 1920

I installed Julia to call from Python (Anaconda). When I try to call PyJulia, I get this error and I am not sure how to fix it:

Python 3.8.3 (default, Jul  2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from julia.api import Julia
>>> jl = Julia(compiled_modules=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Andrea\AppData\Roaming\Python\Python38\site-packages\julia\core.py", line 468, in __init__
    jlinfo = JuliaInfo.load(runtime)
  File "C:\Users\Andrea\AppData\Roaming\Python\Python38\site-packages\julia\juliainfo.py", line 68, in load
    proc = subprocess.Popen(
  File "C:\Users\Andrea\anaconda3\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\Andrea\anaconda3\lib\subprocess.py", line 1307, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified 
1 Answers

This might help:

# IMPORT JULIA
from julia.api import Julia
jpath = "julia-1.3.1/bin/julia" # path to Julia, from current directory (your path may be slightly different)
jl = Julia(runtime=jpath, compiled_modules=False) # compiled_modules=True may work for you; it didn't for me


# IMPORT JULIA MODULES
from julia import Main
Main.include("path/to/some_julia_file.jl")
jl.eval("using .some_julia_module") # use a module from some_julia_file.jl


# EVALUATE STUFF
this_equals_three = jl.eval("1+2")

# eval works with Python f-strings, so you can easily use python variables
a = 1
b = 2
also_three = jl.eval(f"{a}+{b}")

result = jl.eval(f"myfunc({a},{b})") # call custom functions imported from some_julia_file.jl

from julia import Base
equals_one = Base.sind(90)

Note: I assume you installed the PyJulia package using pip install julia, and you also have a separate installation of the Julia language from julialang.org

Related