D. Sukhov
D. Sukhov

Reputation: 361

Call Julia function from a python file

I was able to create a docker environment and then follow this thread I have a high-performant function written in Julia, how can I use it from Python? ..but it seems the suggestion that is made there only works for pure Julia and pure Python projects.

More concretely I created a function diff(img,magnitude) in Julia that changes an image in a certain way and I wanted to implement it into a PyTorch learning algorithm, such that images (let's say from CIFER) would first be changed by the Julia file and then be learned. At the moment I am running into the problem that the Python file does not execute functions with dependencies (Below is the example):

#MyPackage.jl
        
module MyPackage
    
using TestImages
    
function img()
    return imresize(testimage("mandrill"),32,32)
end

end

And in my Jypiter-Notebook I have:

#main.py
!pip install julia

import julia
julia.install()

from julia.api import Julia
jl = Julia(compiled_modules=False)

from julia import Pkg
Pkg.activate("MyPackage")

from julia import MyPackage
MyPackage.img()

When I try to run the Julia function from the Python file I receive this error: enter image description here

Upvotes: 3

Views: 790

Answers (1)

larsks
larsks

Reputation: 311298

With the caveat that I know absolutely nothing about Julia, it looks like imresize is part of the ImageTransformations package, which I think means your Julia code needs:

using ImageTransformations, TestImages

Instead of:

using TestImages

Update

...which means, upon further research, that you also need to add the ImageTransformations package as a dependency to MyPackage. There may be a different way of doing this, but this seemed to work for me:

$ cd MyPackage
$ julia
[...]
(@v1.7) pkg> activate .
  Activating project at `~/tmp/python/MyPackage`

(MyPackage) pkg> add ImageTransformations

Upvotes: 3

Related Questions