A package that allows to use Julia within Python.
Project description
Pyjulia
Pyjulia is a python module for calling julia functions inside your python code.
Usage
The julia interpreter is inside the PATH for the following examples. If you do not have julia inside your PATH you can also define the path to the julia interpreter like that:
import pyjulia
julia_module = pyjulia.Pyjulia("./example.jl")
julia_module.julia_interpreter = "your path to julia interpreter"
# execute code...
You will have to use the Fire package inside of your Julia code in order to use it like a cli application. An example would look like this:
using Fire
@main function add(num::Integer...)
println(sum(num))
end
@main function multiply(num::Integer...)
println(prod(num))
end
@main function greet(name, age, num::Integer...)
println(name, age)
println(num)
end
call_func method
Let's say the content of this julia code belongs to a file called example.jl
. You can now call the add and multiply functions inside your python code:
import pyjulia
julia_module = pyjulia.Pyjulia("./example.jl")
my_args = [2, 3, 5]
julia_sum = julia_module.call_func("add", my_args)
julia_prod = julia_module.call_func("multiply", my_args)
print(julia_sum, julia_prod)
Your output should now look like this:
10 30
using dynamic function declaration
Let's use the same example.jl
from before. This means the content will look like this:
using Fire
@main function add(num::Integer...)
println(sum(num))
end
@main function multiply(num::Integer...)
println(prod(num))
end
@main function greet(name, age, num::Integer...)
println(name, age)
println(num)
end
Now in order to call these function within your python code you need to do this:
⚠️ NOTE: Your IDE/text editor may tell you "Unresolved attribute reference add/multiply for class Julia". There's no need to worry. It should still work just as fine.
import pyjulia
julia_module = pyjulia.Pyjulia("./example.jl")
output = julia_module.add(my_args)
output2 = julia_module.multiply(my_args)
print(output, output2)
Your output should still look like this:
10 30
The greet
function inside the julia file has multiple arguments. But this doesn't prevent us from calling it from python. It works just as fine as the other examples above:
import pyjulia
julia_module = pyjulia.Pyjulia("./example.jl")
greet_args = ["Pyjulia", 19, 2, 3, 4, 5]
greeting = julia_module.greet(greet_args)
print(greeting)
The following output should be printed to the terminal:
pyjulia 19
(2, 3, 4, 5)
You can just treat them as normal positional variables and the Fire package from julia will parse the arguments automatically.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pyjulia-0.0.6.tar.gz
.
File metadata
- Download URL: pyjulia-0.0.6.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6deeae2c4fb500b088e4d7ad52db3c8296e9e50e1fa813afa64886f0a227305f |
|
MD5 | 811f4e589749d72bbcc1d81faf996b4a |
|
BLAKE2b-256 | e21a6ff3e3163cf166c97f8cc31addf389a846487f77f6dbcf35b8543c6f39b7 |
File details
Details for the file pyjulia-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: pyjulia-0.0.6-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a432a8e6ef565320e1cc991946bfe230f1f13b6571c1ec9e34494bd9415c8f2d |
|
MD5 | 335a693b1e7fcd95a00dfe99a31a0b07 |
|
BLAKE2b-256 | 49bb8dadd697f3403cb8cdbfb70fdd9da846cf66cf2c6891292c046a38ed96aa |