Skip to main content

TyJuliaCall

CI codecov versions pypi License

Calling Julia from Python for the real world.

Features:

  1. Cross-platform support for both dynamically linked Python and statically linked Python.

  2. Support Julia system images.

Installation

Prerequisites: Python (>=3.7), Julia (>=1.6)

Then install the tyjuliacall Python package and TyPython Julia package.

pip install -U tyjuliacall
julia -e "import Pkg; Pkg.add(\"TyPython\")"

Julia Initialization Options

Set TYPY_JL_EXTRA_OPTS before the first import of tyjuliacall to pass additional initialization options to Julia:

import os

os.environ["TYPY_JL_EXTRA_OPTS"] = "--quiet"

from tyjuliacall import Base

The value is parsed with shlex.split, combined with TyJuliaCall's required options, and passed to libjulia through its existing jl_parse_opts path. TyJuliaCall reserves --sysimage (-J) and --project; configure a custom system image with use_sysimage() instead. TYPY_JL_OPTS is an internal transport variable and should not be set directly.

This mechanism is for options handled during libjulia initialization. Julia driver actions such as -e, -L, and script arguments do not execute because an embedded Julia instance does not run the command-line driver's _start(). The standalone -- separator is not supported. Invalid or process-exiting options are handled by Julia itself and can make the Python import fail or terminate.

Shutting Down Julia

TyJuliaCall shuts down the embedded Julia runtime when Python exits normally. This runs Julia atexit hooks and pending finalizers.

Forced process termination, including os._exit(), does not run exit hooks.

Using System Images

from tyjuliasetup import use_sysimage  # CAUTIOUS: not 'tyjuliacall'!
use_sysimage(r"/path/to/sysimg")
# if your sysimage contains TyPython,
# you could call use_system_typython() to reduce the time cost of setting up julia.
from tyjuliacall import Base
print(
    "current sysimage in use",
    Base.unsafe_string(Base.JLOptions().image_file))
# out: /path/to/sysimg

受信赖的Python-Julia数据类型转换

虽然tyjuliacall允许在Python和Julia之间传递任意数据,但由于是两门不同的语言,数据转换的类型对应关系是复杂的。

为了保证代码的后向兼容性,使得规范的代码在不同版本的Syslab/tyjuliacall上都可以运行,建议只使用如下的数据类型转换。

Python数据传递到Julia

Python向Julia函数传参时,推荐只使用下表左边的数据类型,以保证代码的后向兼容。

Python Type Julia Type
基本类型
int Int64
float Float64
bool Bool
complex ComplexF64
None nothing
str String
组合类型
numpy.ndarray (dtype为数字或字符串或bool) 原生Array
tuple,且元素均为表中数据类型 Tuple

对于Python传递给Julia的tuple,其各个元素按照以上规则依次转换。

TIPS: 如何传递bytearray或者bytes到Julia?

  1. 向Julia函数传递bytes时,可以改为传递一个uint8的数组。

    无拷贝传参: np.array(memoryview(b'mybytes'), dtype=np.uint8) 拷贝传参: np.array(list(b'mybytes'), dtype=np.uint8)

  2. 向Julia函数传递bytearray时,可以改为传递一个uint8的数组。

    无拷贝传参: np.asarray(bytearray(b'mybytes')))

Julia数据传递到Python

当获取Julia函数返回值,或导入Julia模块的非函数对象时,将发生Julia到Python的数据传递。

保证后向兼容的Julia到Python数据转换关系如下表所示:

Julia Python
基本类型
Integer子类型 int
AbstractFloat子类型 float
Bool bool
Complex子类型 complex
nothing对象 None
AbstractString子类型 str
Vector{UInt8} bytearray
组合类型
AbstrctArray{T} (T见下方说明) numpy.ndarray
Tuple{T1, ..., Tn}, 且Ti为该表中的类型 tuple
其余Julia类型 tyjuliacall.JV

一个Julia AbstrctArray能转换为numpy数组,当且仅当其元素类型T是以下类型之一

  • Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64
  • Float16, Float32, Float64
  • ComplexF16, ComplexF32, ComplexF64
  • Bool

注意,当类型为Vector{String}或者Array{String, 2}的Julia对象被返回给Python时,它被封装为一个tyjuliacall.JV类型。

其他说明

  1. 不要对Julia包/模块使用from ... import *。
  2. Vector{String}传到Python是一个tyjuliacall.JV,这是一个纯Julia对象的包装,因此下标索引是从1开始的。

Metadata

Release files for tyjuliacall 0.7.8

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

Source distribution (sdist)

Source distribution for tyjuliacall 0.7.8
File Size Uploaded Provenance Actions
tyjuliacall-0.7.8.tar.gz 17.6 kB Without provenance Details for tyjuliacall-0.7.8.tar.gz

Built distribution (wheel)

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

Total release size: 36.6 kB

Release files / tyjuliacall-0.7.8.tar.gz

Download URL tyjuliacall-0.7.8.tar.gz
Size 17.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6b0a0728e71af9a6f26dcd6a522df35df69dd8b59a33c8590c497c68e3c1ef51
BLAKE2b-256 checksum
How to use checksums
dd52dd63a2b52349ff295b3a228d092a3662a0419df566f7cd40cb1a7da4586c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / tyjuliacall-0.7.8-py3-none-any.whl

Download URL tyjuliacall-0.7.8-py3-none-any.whl
Size 19.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
08421a207dd417c54071e2958906141c9ff61c5001c6dfe3382e7ade2e69a880
BLAKE2b-256 checksum
How to use checksums
e5ae8e31bedbeef1b9afd8c4e0fba8bb717371435039c90b8443ca0543487521
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Security for tyjuliacall 0.7.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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