Can't run a Python code on Julia using PyCall

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:

using PyCall

export smith_normal_form


from ctypes import *
import numpy as np

polylib = cdll.LoadLibrary("")

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:

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

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

np = pyimport("numpy")
1 Like
  • 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.


for the python code it’s working, as for running the code using the julia path/to/script.jli’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

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.

1 Like

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

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.

1 Like