# Can't run a Python code on Julia using PyCall

**URL:** <https://discourse.julialang.org/t/cant-run-a-python-code-on-julia-using-pycall/29061>\
**Category:** General Usage\
**Tags:** pycall\
**Created:** [September 23, 2019, 7:18am UTC](https://discourse.julialang.org/t/cant-run-a-python-code-on-julia-using-pycall/29061 "2019-09-23T07:18:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)\
**Post date:** [September 23, 2019, 7:18am UTC](https://discourse.julialang.org/t/cant-run-a-python-code-on-julia-using-pycall/29061/1 "2019-09-23T07:18:29Z")

</div>

Hi everybody,  
since Julia is able to connect with Python using PyCall i was trying to apply that on a python code on julia for a smith decomposition but i can’t run it even tho everything seem fine to me, here’s the code that i’m trying to run:

```nohighlight
using PyCall

export smith_normal_form

py"""

from ctypes import *
import numpy as np

polylib = cdll.LoadLibrary("libpolylib64.so")

class Matrix(Structure):
    _fields_ = [
        ("NbRows", c_uint32),
        ("NbColumns", c_uint32),
        ("p", POINTER(POINTER(c_int64))),
        ("p_Init", POINTER(c_int64)),
        ("p_Init_size", c_int32)
    ]

def smith_normal_form(A):
    pM0 = polylib.Matrix_Alloc(3, 3)
    M0 = Matrix.from_address(pM0);
    pM = pointer(M0)
    pU = POINTER(Matrix)()
    pV = POINTER(Matrix)()
    pP = POINTER(Matrix)()
    M = np.ctypeslib.as_array(M0.p_Init, shape=(3,3))
    M[:] = A
    polylib.Smith.argtypes=[POINTER(Matrix), POINTER(POINTER(Matrix)), POINTER(POINTER(Matrix)), POINTER(POINTER(Matrix))]
    polylib.Smith(pM, pointer(pU), pointer(pV), pointer(pP))
    U = np.ctypeslib.as_array(pU.contents.p_Init, shape=(3,3))
    V = np.ctypeslib.as_array(pV.contents.p_Init, shape=(3,3))
    P = np.ctypeslib.as_array(pP.contents.p_Init, shape=(3,3))
  
    return U, P, V
"""

A =py"np.array"([[1,2,3],[-3,2,0],[1,0,0]])

U, P, V =py"smith_normal_form"(A) 

```

and after i run it, it shows me this error:

```julia
ERROR: PyError ($(Expr(:escape, :(ccall(#= /Users/midow/.julia/packages/PyCall/0jMpb/src/pyeval.jl:23 =# @pysym(:PyEval_EvalCode), PyPtr, (PyPtr, PyPtr, PyPtr), o, globals, locals))))) <class 'NameError'>
NameError("name 'np' is not defined")
  File "/Users/midow/.julia/packages/PyCall/0jMpb/src/pyeval.jl", line 1, in <module>
    const Py_single_input = 256 # from Python.h

```

i’m using Julia 1.0.1

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 23, 2019, 10:47am UTC](https://discourse.julialang.org/t/cant-run-a-python-code-on-julia-using-pycall/29061/2 "2019-09-23T10:47:48Z")

</div>

Did you try to put the Python code in a separate file?  
And to do the import of numpy correctly:

```julia
np = pyimport("numpy")

```

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [September 23, 2019, 6:44pm UTC](https://discourse.julialang.org/t/cant-run-a-python-code-on-julia-using-pycall/29061/3 "2019-09-23T18:44:46Z")

</div>

- Do you need all those code to invoke the error? The error `NameError("name 'np' is not defined")` sounds like you probably can get the same error with `py"np.array"`.

- How do you run this code? As a single script file? If so is it via `include` in REPL? From command line like `julia path/to/script.jl` (unless you know what you are doing, I highly recommend this when you are asking for a help or reporting an issue)? Or is it in a module? These things sometimes matter when using PyCall and especially `py"..."` macro.

- Did you try calling `smith_normal_form` inside the `py"..."` macro defining it (i.e., as a pure Python program)? Did it work?

- Also, please always include full stacktrace unless you are very sure that they are irrelevant. Some of the questions above can be answered by looking at the stacktrace.

---

<div class="post-metadata">

**Author:** ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)\
**Post date:** [September 24, 2019, 8:43am UTC](https://discourse.julialang.org/t/cant-run-a-python-code-on-julia-using-pycall/29061/4 "2019-09-24T08:43:37Z")

</div>

for the python code it’s working, as for running the code using the `julia path/to/script.jl`i’ve already tried it , the whole thing is just a code for smith decomposition that i’m trying to include it in another code, i should’ve tried the first answer on this post as it is the convinient one

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [September 24, 2019, 11:26am UTC](https://discourse.julialang.org/t/cant-run-a-python-code-on-julia-using-pycall/29061/5 "2019-09-24T11:26:30Z")

</div>

I’m not sure exactly what the problem is, but FWIW I copied and pasted your exact code into a juliabox notebook (minus the DLL call since it did not exist), and it seemed to work as I did not get the “np.array” error. For reference, juliabox is running v1.0.3.

One thing I have found is that the file Python.h is supplied by the Python development libraries, so make sure you have those installed on your machine. They should be under something like python-devel or python3-devel, depending on what version you’re running.

---

<div class="post-metadata">

**Author:** ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)\
**Post date:** [September 24, 2019, 11:32am UTC](https://discourse.julialang.org/t/cant-run-a-python-code-on-julia-using-pycall/29061/6 "2019-09-24T11:32:18Z")

</div>

yeah i had a problem at first in installing the Libraries in macbook since i didn’t find a macbook version of those libraries but when i switched to Linux everything was good so the python code was run but when i used the PyCall and tried to call the function it didn’t work but apparently, if you do `np = pyimport("numpy")`you have no longer the error

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [September 24, 2019, 9:40pm UTC](https://discourse.julialang.org/t/cant-run-a-python-code-on-julia-using-pycall/29061/7 "2019-09-24T21:40:57Z")

</div>

> [@marouane](#):
>
> if you do `np = pyimport("numpy")` you have no longer the error

It is unlikely this fixes the original issue. There is no `np` in Julia’s namespace in your original code. Furthermore, the error in the OP is thrown in Python side; i.e., `np` is missing in Python’s namespace.

I’d assume you did something else that accidentally fixed the original problem.
