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)
Calling Julia from Python Julia functions get converted to callable Python objects, so you can easily call Julia from Python via callback function arguments. The pyjulia module allows you to call Julia directly from Python, and also uses PyCall to do its conversions.
1.6. For this, we will use the integrated terminal in VS Code. Recall that you can start this directly from the command palette with <Ctrl+Shift+P> then typing part of the > Julia: Start REPL command.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With