PyJulia
repository·master·Indexed 21 days ago
https://github.com/juliapy/pyjuliaA Python 3 interface to the Julia programming language (v1.0+) that allows developers to call Julia functions and modules directly from Python. It provides the julia.api.Julia class for session management, julia.api.LibJulia for low-level access, and a pytest plugin for Julia-specific testing. Note: Ongoing development has transitioned to PythonCall.jl/juliacall, which is recommended for new projects.
What's inside pyjulia
- PyJulia provides a Python 3 interface to the Julia language (v1.0+). It allows developers to bridge Python and Julia, enabling the use of Julia's computational capabilities within Python environments. PyJulia is compatible with Python 3.5 and newer.
Use a custom Julia system image to avoid precompilation issues
masterInstead of disabling the compilation cache (which is slow), you can create a custom Julia system image. This avoids precompilation cache issues and makes PyJulia initialization nearly instant. Refer to thesysimagedocumentation for specific instructions on how to create and use one.Understanding GIL and Parallelism limitations
masterPyJulia does not release the Global Interpreter Lock (GIL) while calling Julia functions. This is because PyCall requires the GIL to be acquired at all times. As a result, Python code and Julia code cannot run in parallel; they will execute sequentially.Use PyJulia in Python virtual environments
masterPyJulia is compatible with virtual environments created by
virtualenv,venv, andpipenv.Requirement: The Python executable used in the virtual environment must be linked to the same
libpythonused byPyCall. If there is a mismatch, PyJulia will print an error message detailing the detected paths tolibpythonduring initialization.Warning: Python environments created by
condaare not supported.Handling Unicode identifier mismatches
masterPyJulia cannot access Julia methods or variables that use non-ASCII Unicode identifiers if they are not compatible with Python's identifier rules. Even in Python 3, where Unicode is supported, Python's aggressive normalization can cause mismatches. For example,ϵ(GREEK LUNATE EPSILON SYMBOL) andε(GREEK SMALL LETTER EPSILON) are treated as identical in Python 3 but are distinct in Julia. Avoid using highly specialized Unicode characters in Julia code if you need to access them via PyJulia.How PyJulia manages memory and object conversion
masterPyJulia operates by loading the
libjulialibrary and executing statements directly within it.To facilitate communication between Python and Julia, PyJulia utilizes the Julia
PyCallpackage for variable conversion. Memory management is handled through a coordinated reference counting mechanism:- Python Side: Python maintains reference counts for Python references to Julia objects.
- Julia Side: These objects are retained in the
PyCall.pycall_gcmapping on the Julia side. - Cleanup: When a Python reference count drops to zero, the entry is removed from the
PyCall.pycall_gcmapping, allowing the underlying Julia object to be garbage collected.
Migration notice: Use PythonCall.jl/juliacall instead
masterOngoing development of the Python/Julia interface has transitioned to PythonCall.jl/juliacall. It is recommended to usejuliacallfor new projects instead ofpyjulia.Use the Low-level interface for custom Julia runtimes
masterIf your Julia executable is not in your
PATHor you need to use a specific version (e.g.,custom_julia), you must use the low-level interface.Crucial: You must initialize the
Juliaobject with your custom runtime before importing any other Julia modules (likejulia.Baseorjulia.Main).from julia import Julia # Initialize before importing other julia modules jl = Julia(runtime="custom_julia") from julia import BaseUse the `python-jl` command for basic use-cases
masterPyJulia bundles a
python-jlcommand that launches the Python interpreter inside Julia. This is an easy workaround for basic scripts or interactive sessions.Requirement: PyJulia must be installed in the Python environment that
PyCallis configured to use. You can verify this in the Julia REPL by running:using PyCall pyimport("julia") # Should return a PyObject for the 'julia' module$ python-jl your_script.py $ python-jl -c 'from julia.Base import banner; banner()' $ python-jl -m IPythonUse IPython/Jupyter %julia magic
masterIn IPython or Jupyter environments, you can execute Julia code directly using the
%juliamagic command.Accessing Python variables in Julia
- Use
$varto access a single Python variable. - Use
py"..."for more complex Python expressions.
Escaping Python calls
Inside strings, quote blocks, or macro arguments,
$varandpy"..."behave like standard Julia syntax. To force a Python call in these contexts, escape the symbol one extra time (e.g.,\$varor\py"...").Type Conversion
Results are automatically converted between equivalent Python and Julia types. To prevent this conversion and keep the result as a
PyObject, appendoto the Python string (e.g.,py"1"o).# Load the extension %load_ext julia.magic # Execute Julia code %julia [1 2; 3 4] .+ 1 # Access Python variables arr = [1, 2, 3] %julia $arr .+ 1 %julia sum(py"[x**2 for x in arr]") # Escaping Python calls in strings foo = "Python" %julia ("this is $foo", "this is $($foo)") # Returns 'this is Python' %julia ("this is \$foo", "this is \$(\$(foo))") # Returns 'this is Python' (escaped) # Disabling automatic type conversion %julia typeof(py"1"), typeof(py"1"o)- Use
Quick start with PyJulia
masterAfter installing the package, you must run
julia.install()to set up the necessary Julia components likePyCall.jl. Once installed, you can import Julia modules directly into Python. For example, you can access theBasemodule from Julia and call functions likesind.import julia # Install PyCall.jl and other dependencies julia.install() # Import a Julia module from julia import Base # Call a Julia function result = Base.sind(90) print(result) # Output: 1.0Run PyJulia tests using tox
masterPyJulia's test suite is managed viatox. You can run the full test suite by executingtoxin your terminal. You can pass additional arguments topytestby using a double dash--separator.$ tox