Answer a question

I'm trying to run a Julia .jl file in Python, however, after having tried different options none of them is working.

I've tried to use PyJulia. Import Julia and define a Julia object. Not achieved.

Has anyone mix Python and Julia technologies and has succeeded? (run Julia from Python)

Answers

First install PyCall package in Julia by running Pkg.add("PyCall") in Julia REPL.

Next you need to install julia for Python:

$ pip install julia

should work. Here is the output from my console (you should see something similar):

$ pip install julia
Collecting julia
  Downloading julia-0.1.5-py2.py3-none-any.whl (222kB)
    100% |████████████████████████████████| 225kB 1.1MB/s
Installing collected packages: julia
Successfully installed julia-0.1.5

Now assume you have the following file test.jl in your working directory:

for i in 1:10
    println(i)
end
1+2

(it should print numbers from 1 to 10, and return value 3 which is the result of sum of 1 and 2).

Now you start Python REPL and use julia package as follows to run a custom Julia script:

>>> import julia
>>> j = julia.Julia()
>>> x = j.include("test.jl")
1
2
3
4
5
6
7
8
9
10
>>> x
3

And as you can see you have the return value of Julia script assigned to variable x in Python.

You can find more details here: https://github.com/JuliaPy/pyjulia.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐