Is it possible to write julia code in python and run it?
Asked Answered
T

2

5

I want to write the julia code in the .py file, and when running the py file, the julia code can also pass normally, is this possible? I am using text editor VScode, python3.10 and julia1.7.2.

Actually, I know it's a bit whimsical, but today I saw python code running successfully in julia. So I wonder if it can be reversed.

module MyModule
using PyCall
function __init__()
    py"""
    import numpy as np
    def one(x):
        return np.sin(x) ** 2 + np.cos(x) ** 2
    """
end
two(x) = py"one"(x) + py"one"(x)
end
MyModule.two(2)
Tontine answered 21/5, 2022 at 8:51 Comment(5)
It's unclear what you're really trying to do, or why. Please explain in more detail. Obviously the Python interpreter won't understand Julia as they're different languages. What are you actually trying to achieve?Expense
docs.julialang.org/en/v1/manual/embedding . Which mentions in the first paragraph: "the Julia C API can also be used to build further language bridges (e.g. calling Julia from Python or C#)."Oboe
So the answer to your question is "Yes".Oboe
"I am using text editor VScode": that is of absolutely no importance. Your OS might be more important (but unlikely).Oboe
I think this could be considered a duplicate of: #64241764Cadmar
M
6

Have a look at PyJulia.

from julia import Main
Main.eval("sin.(xs)")
Macilroy answered 21/5, 2022 at 9:52 Comment(2)
As an addition this is a nice post about it blog.esciencecenter.nl/…Lan
Also, all details you will need in the section Using Julia in Python of this course.Icecold
S
2

JuliaCall is another possible solution.

from juliacall import Main as jl
jl.println("Hello from Julia!")
Sleepwalk answered 21/5, 2022 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.