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)
Leo
  • 113
  • 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? – ndc85430 May 21 '22 at 08:53
  • https://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#)." – 9769953 May 21 '22 at 09:48
  • So the answer to your question is "Yes". – 9769953 May 21 '22 at 09:48
  • "I am using text editor VScode": that is of absolutely no importance. Your OS might be more important (but unlikely). – 9769953 May 21 '22 at 09:49
  • I think this could be considered a duplicate of: https://stackoverflow.com/questions/64241264/i-have-a-high-performant-function-written-in-julia-how-can-i-use-it-from-python/64241265 – Przemyslaw Szufel May 21 '22 at 14:43

2 Answers2

6

Have a look at PyJulia.

from julia import Main
Main.eval("sin.(xs)")
AlexApps99
  • 3,506
  • 9
  • 21
  • As an addition this is a nice post about it https://blog.esciencecenter.nl/how-to-call-julia-code-from-python-8589a56a98f2 – Bogumił Kamiński May 21 '22 at 10:18
  • Also, all details you will need in the section [Using Julia in Python](https://sylvaticus.github.io/SPMLJ/stable/01_-_JULIA1_-_Basic_Julia_programming/0106_-_Further_topics.html#Using-Julia-in-Python) of this course. – Antonello May 21 '22 at 21:17
2

JuliaCall is another possible solution.

from juliacall import Main as jl
jl.println("Hello from Julia!")
Tyler Thomas
  • 21
  • 1
  • 1