Skip to main content

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.

Metadata

Release files for pyjulia 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyjulia 0.0.6
File Size Uploaded
pyjulia-0.0.6.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyjulia 0.0.6
File Interpreter ABI Platform
pyjulia-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 16.4 kB

Release files / pyjulia-0.0.6.tar.gz

Download URL pyjulia-0.0.6.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6deeae2c4fb500b088e4d7ad52db3c8296e9e50e1fa813afa64886f0a227305f
BLAKE2b-256 checksum
How to use checksums
e21a6ff3e3163cf166c97f8cc31addf389a846487f77f6dbcf35b8543c6f39b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.3

Release files / pyjulia-0.0.6-py3-none-any.whl

Download URL pyjulia-0.0.6-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a432a8e6ef565320e1cc991946bfe230f1f13b6571c1ec9e34494bd9415c8f2d
BLAKE2b-256 checksum
How to use checksums
49bb8dadd697f3403cb8cdbfb70fdd9da846cf66cf2c6891292c046a38ed96aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.3

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page