Using a function in PyJulia

I want to use a function defined in a Julia file with PyJulia. Running the code gives the following error:

RuntimeError: <PyCall.jlwrap (in a Julia function called from Python)
JULIA: UndefVarError: pqrfact not defined

The code in Julia is
% function.jl

import LowRankApprox
import Pkg
function f(A)
F = pqrfact(A)
B = A * F[:P]
return B
function h(A)
F = pqrfact(A)
z = F[:k]
return z

In Julia, this code runs perfectly.

The code in Python is:

import numpy as np
import julia
from julia.api import Julia
jl = Julia(compiled_modules=False)
j = julia.Julia()
B = np.array([[1, 2, 3], [2, 1.0, 1], [1, 1, 1]])
print('B * F[: P] 1, j.f(B))
print('posto_numerico(B)= ', j.h(B))

Any help is welcome. Thank you in advance.

You are importing the package LowRankApprox before adding it. Try moving the import statement after the Pkg.add.

1 Like

I put the import after Pkg.add and still giving the same error. It looks like it’s the way I defined the function.

I would take the time to learn Julia properly because these scripts aren’t ideal. The import in Julia doesn’t bring the function names such as pqrfact into scope, which is available inside LowRankApprox I am assuming. Are you sure your script works in Julia?

Also, you don’t Pkg.add("LowRankApprox") every time you run a script. This is a command that you run once when setting up the environment.

Please take the time to read the Julia documentation if you plan to use Julia more often in your work.

1 Like

I managed to solve the problem. The error was because I used import instead of using in the Julia code. :upside_down_face:

1 Like