# Using a function in PyJulia

**URL:** <https://discourse.julialang.org/t/using-a-function-in-pyjulia/49330>\
**Category:** General Usage\
**Created:** [October 30, 2020, 8:33pm UTC](https://discourse.julialang.org/t/using-a-function-in-pyjulia/49330 "2020-10-30T20:33:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rita\_Paz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rita_paz/32/19049_2.png) [@Rita\_Paz](https://discourse.julialang.org/u/Rita_Paz)\
**Post date:** [October 30, 2020, 8:33pm UTC](https://discourse.julialang.org/t/using-a-function-in-pyjulia/49330/1 "2020-10-30T20:33:40Z")

</div>

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  
> Pkg.add(“LowRankApprox”)  
> function f(A)  
> F = pqrfact(A)  
> B = A \* F[:P]  
> return B  
> end  
> function h(A)  
> F = pqrfact(A)  
> z = F[:k]  
> return z  
> end

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()  
> j.include(‘function.jl’)  
> 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.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [October 30, 2020, 9:03pm UTC](https://discourse.julialang.org/t/using-a-function-in-pyjulia/49330/2 "2020-10-30T21:03:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rita\_Paz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rita_paz/32/19049_2.png) [@Rita\_Paz](https://discourse.julialang.org/u/Rita_Paz)\
**Post date:** [October 31, 2020, 1:43pm UTC](https://discourse.julialang.org/t/using-a-function-in-pyjulia/49330/3 "2020-10-31T13:43:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 31, 2020, 2:16pm UTC](https://discourse.julialang.org/t/using-a-function-in-pyjulia/49330/4 "2020-10-31T14:16:50Z")

</div>

> [@Rita\_Paz](#):
>
> import LowRankApprox  
> import Pkg  
> Pkg.add(“LowRankApprox”)  
> function f(A)  
> F = pqrfact(A)  
> B = A \* F[:P]  
> return B  
> end  
> function h(A)  
> F = pqrfact(A)  
> z = F[:k]  
> return z  
> end

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](https://docs.julialang.org/en/v1) if you plan to use Julia more often in your work.

---

<div class="post-metadata">

**Author:** ![Rita\_Paz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rita_paz/32/19049_2.png) [@Rita\_Paz](https://discourse.julialang.org/u/Rita_Paz)\
**Post date:** [October 31, 2020, 8:09pm UTC](https://discourse.julialang.org/t/using-a-function-in-pyjulia/49330/5 "2020-10-31T20:09:25Z")

</div>

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