【问题标题】:How to load in Python a custom Julia package using Python's juliacallHow to load in Python a custom Julia package using Python\'s juliacall
【发布时间】:2022-12-27 23:03:17
【问题描述】:

I have created a simple Julia package with the following command: using Pkg;Pkg.generate("MyPack");Pkg.activate("MyPack");Pkg.add("StatsBase") where the file MyPack/src/MyPack.jl has the following contents:

module MyPack
using StatsBase

function f1(x, y)
   return 3x + y
end
g(x) = StatsBase.std(x)

export f1

end

Now I would like to load this Julia package in Python via juliacall and call f1 and g functions. I have already run pip3 install juliacall from command line. How do I call the above functions from Python?

【问题讨论】:

    标签: python julia


    【解决方案1】:

    You need to run the following code to load the MyPack package from Python via juliacall

    from juliacall import Main as jl
    from juliacall import Pkg as jlPkg
    
    jlPkg.activate("MyPack")  # relative path to the folder where `MyPack/Project.toml` used here 
    
    jl.seval("using MyPack")
    

    Now you can use the function (note that calls to non exported functions require package name):

    jl.f1(4,7)
    19
    
    >>> jl.f1([4,5,6],[7,8,9]).to_numpy()
    array([19, 23, 27], dtype=object)
    
    >>> jl.MyPack.g(numpy.arange(0,3))
    1.0
    

    【讨论】:

      猜你喜欢
      • 2022-12-26
      • 2022-12-26
      • 2022-12-02
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      • 2022-12-01
      • 2022-12-01
      • 2022-12-02
      相关资源
      最近更新 更多