juliacall

0.9.31 · active · verified Fri Apr 17

juliacall is a Python library that enables seamless interoperability between Python and Julia, allowing Python code to call Julia functions, access Julia variables, and evaluate Julia code. It's part of the broader PythonCall.jl project which provides bidirectional integration. The library is actively maintained with frequent updates, often several minor releases per month.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a Julia session, evaluate Julia code, access Julia variables, and call both built-in and custom Julia functions from Python using juliacall.

from juliacall import Julia
import os

# For robust demos, ensure Julia is found (optional, but good practice if not on PATH)
# os.environ['JULIA_BIN'] = '/path/to/your/julia/bin/julia' # Uncomment and modify if needed

try:
    jl = Julia() # Initializes a Julia session
    
    # Evaluate Julia code
    jl.seval("x = 10; y = 20")
    print(f"Julia variable x: {jl.x}")
    print(f"Julia variable y: {jl.y}")
    
    # Call a Julia function
    result = jl.cos(jl.pi) # Access Julia's pi and cos function
    print(f"cos(pi) from Julia: {result}")

    # Define and call a custom Julia function
    jl.seval("my_add(a, b) = a + b")
    custom_result = jl.my_add(5, 7)
    print(f"Custom Julia function my_add(5, 7): {custom_result}")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure Julia is installed and discoverable (e.g., on PATH or via JULIA_BIN env var).")

view raw JSON →