juliacall
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
-
JuliaError: Couldn't find Julia. Ensure Julia is installed and on your PATH, or set the JULIA_BIN environment variable.
cause The juliacall library could not locate the Julia executable on your system.fixInstall Julia from https://julialang.org/downloads/. Add Julia's `bin` directory to your system's PATH, or set the `JULIA_BIN` environment variable to the full path of the Julia executable (e.g., `export JULIA_BIN=/opt/julia-1.9.3/bin/julia`). -
ModuleNotFoundError: No module named 'juliacall'
cause The juliacall Python package is not installed in your current Python environment.fixRun `pip install juliacall` in your terminal to install the package. -
AttributeError: 'Julia' object has no attribute 'my_julia_function'
cause The Julia function or variable you are trying to access has not been defined in the current Julia session or is not in the scope of the `Julia` or `Main` object you are using.fixEnsure the Julia code defining `my_julia_function` (e.g., via `jl.seval("my_julia_function(x) = x^2")`) has been executed *before* attempting to call it. Remember that `Julia()` instances are isolated, while `Main` shares the global Julia environment.
Warnings
- gotcha juliacall requires a separate Julia installation on your system. It is not automatically installed via pip and will not function without a correctly configured Julia runtime.
- gotcha Instantiating `juliacall.Julia()` creates a new, isolated Julia session, which can be slow and memory-intensive upon first call. For accessing an existing Julia environment or global state, `juliacall.Main` is often more appropriate and performs better after the initial load.
- gotcha Automatic type conversions between Python and Julia can sometimes lead to unexpected behavior or performance overhead, especially for complex or mutable data structures. For example, Python lists might convert to Julia `Vector{Any}` which can be less performant than a specific `Vector{Int}`.
Install
-
pip install juliacall
Imports
- Julia
from juliacall import Julia
- Main
from juliacall import Main
Quickstart
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).")