Julia/Python bridge with IPython support enabling seamless interoperability between Python and Julia programming languages.
—
Primary interface for executing Julia code from Python, providing runtime initialization, module importing, function calling, and automatic type conversion between Python and Julia data structures.
Initialize and configure the Julia runtime with various options for optimization, compilation, and system configuration.
class Julia:
def __init__(self, init_julia=True, jl_init_path=None, runtime=None,
jl_runtime_path=None, debug=False, **julia_options):
"""
Create a Python object that represents a live Julia runtime.
Parameters:
- init_julia: bool, whether to initialize Julia runtime (default: True)
- jl_init_path: Path to Julia initialization script (deprecated)
- runtime: str, custom Julia binary path
- jl_runtime_path: Path to Julia runtime (deprecated)
- debug: bool, enable debugging information
- **julia_options: Additional Julia options including:
- sysimage: Path to custom Julia system image
- bindir: Julia binary directory path
- compiled_modules: Module compilation setting ('yes'/'no')
- compile: Compilation level ('yes'/'no'/'all'/'min')
- depwarn: Deprecation warnings ('yes'/'no'/'error')
- optimize: Optimization level (0/1/2/3)
- threads: Number of Julia threads (int or 'auto')
- inline: Inline function calls ('yes'/'no')
- check_bounds: Array bounds checking ('yes'/'no')
- warn_overwrite: Method overwrite warnings ('yes'/'no')
- min_optlevel: Minimum optimization level (0/1/2/3)
"""
class LegacyJulia(Julia):
"""Legacy wrapper around Julia class for backward compatibility."""Execute Julia code and expressions directly from Python with automatic type conversion.
def eval(self, code: str):
"""
Evaluate Julia code and return the result.
Parameters:
- code: Julia code string to execute
Returns:
- Result of Julia expression with automatic Python type conversion
"""
def using(self, module_name: str):
"""
Load a Julia module (equivalent to 'using ModuleName' in Julia).
Parameters:
- module_name: Name of Julia module to load
"""Access Julia modules directly as Python objects with automatic function and constant exposure.
class JuliaModule:
"""
Represents a Julia module accessible from Python.
Provides attribute access to Julia functions and constants.
"""
class JuliaMainModule(JuliaModule):
"""
Special Julia Main module with assignment support.
Allows setting Julia variables from Python.
"""
class JuliaImporter:
"""Meta path finder for importing Julia modules."""
class JuliaModuleLoader:
"""Loader implementation for Julia modules."""
# Direct module importing
from julia import Base # Julia's Base module
from julia import Main # Julia's Main module
from julia import Pkg # Julia's package managerRetrieve detailed information about the Julia installation and runtime configuration.
class JuliaInfo:
"""
Information required for initializing Julia runtime.
Contains paths, version info, and compatibility checks.
Attributes:
- julia: str, path to Julia executable
- bindir: str, Julia's Sys.BINDIR path
- libjulia_path: str, path to libjulia shared library
- sysimage: str, path to Julia system image
- python: str, Python executable used by PyCall.jl
- libpython_path: str, libpython path used by PyCall.jl
- version_info: tuple, Julia version information
- version_raw: str, raw Julia version string
"""
@classmethod
def load(cls, julia="julia", **popen_kwargs):
"""
Get basic information from Julia executable.
Parameters:
- julia: str, path to Julia executable (default: "julia")
- **popen_kwargs: Additional arguments for subprocess.Popen
Returns:
- JuliaInfo instance with system information
"""
def is_pycall_built(self) -> bool:
"""Check if PyCall.jl is properly built."""
def is_compatible_python(self) -> bool:
"""Check if current Python is compatible with PyCall.jl configuration."""Install and configure Julia packages required for PyJulia operation.
def install(julia="julia", color="auto", python=None, quiet=False):
"""
Install/configure Julia packages required by PyJulia.
Parameters:
- julia: Path to Julia executable (default: "julia")
- color: Color output setting ("auto"/"yes"/"no")
- python: Python executable path (default: current Python)
- quiet: Suppress output if True
"""Configure logging, debugging, and runtime behavior for troubleshooting and development.
def enable_debug():
"""Enable debug-level logging for troubleshooting."""
def set_loglevel(level):
"""
Set PyJulia's logging level.
Parameters:
- level: Logging level (DEBUG/INFO/WARNING/ERROR)
"""
def get_loghandler():
"""Get PyJulia's logging StreamHandler instance."""Enable or disable Revise.jl integration for automatic code reloading during development.
def enable_revise():
"""(Re-)enable Revise.jl integration for development workflows."""
def disable_revise():
"""Disable Revise.jl integration for development workflows."""Convert between Python and Julia identifier naming conventions.
def jl_name(name: str) -> str:
"""
Convert Python identifier to Julia identifier.
Handles '_b' suffix → '!' conversion.
"""
def py_name(name: str) -> str:
"""
Convert Julia identifier to Python identifier.
Handles '!' → '_b' suffix conversion.
"""
def ismacro(name: str) -> bool:
"""Check if name represents a Julia macro."""
def isoperator(name: str) -> bool:
"""Check if name represents a Julia operator."""
def is_accessible_name(name: str) -> bool:
"""Check if Julia name is accessible from Python."""from julia import Julia
# Initialize Julia
jl = Julia()
# Execute Julia code
result = jl.eval('2 + 3')
print(result) # 5
# Use Julia's mathematical functions
jl.eval('using Statistics')
data = jl.eval('[1, 2, 3, 4, 5]')
mean_val = jl.eval('mean([1, 2, 3, 4, 5])')
print(mean_val) # 3.0from julia import Base, Main
# Call Julia Base functions
sin_result = Base.sin(3.14159/2)
print(sin_result) # ≈1.0
# Access Julia constants
pi_val = Base.pi
print(pi_val) # 3.141592653589793
# Use Main module for custom variables
Main.my_var = [1, 2, 3]
result = Main.eval('sum(my_var)')
print(result) # 6from julia import Julia
# Configure Julia with specific options
jl = Julia(
sysimage="/path/to/custom.so",
threads=4,
optimize=2,
compiled_modules="yes"
)
# Enable debugging for troubleshooting
from julia.core import enable_debug
enable_debug()
# Execute with debugging enabled
result = jl.eval('println("Debug mode active")')class JuliaError(Exception):
"""
Wrapper for Julia exceptions raised during execution.
Contains the original Julia error information.
"""
class JuliaNotFound(Exception):
"""Raised when Julia executable cannot be found."""
class UnsupportedPythonError(Exception):
"""Raised for unsupported Python configurations."""Example error handling:
from julia import Julia, JuliaError
jl = Julia()
try:
result = jl.eval('undefined_function()')
except JuliaError as e:
print(f"Julia error: {e}")
# Handle Julia-specific errors
except Exception as e:
print(f"Other error: {e}")Install with Tessl CLI
npx tessl i tessl/pypi-julia