@pyimport numpy throwing an error when precompiling a module

Hi everyone !

I am running on a Mac (with M1 chip) and I am quite new to Julia. I am currently modifying an existing Julia package. I want to call/write some python code requiring importing numpy. However when precompiling (or executing a script involving that package I modified), the following line

@pyimport numpy as np

throw me the error

ERROR: LoadError: PyError (PyImport_ImportModule) <class 'RuntimeError'>
RuntimeError("Polyfit sanity test emitted a warning, most likely due to using a buggy Accelerate backend.\nIf you compiled yourself, more information is available at:\nhttps://numpy.org/doc/stable/user/building.html#accelerated-blas-lapack-libraries\nOtherwise report this to the vendor that provided NumPy.\nUserWarning: The value of the smallest subnormal for <class 'numpy.float64'> type is zero.\n")
  File "/Users/jcrismer/.julia/conda/3/lib/python3.9/site-packages/numpy/__init__.py", line 376, in <module>
    raise RuntimeError(msg)

On the other hand, when I just write a script (outside of that package I am editing) I can import numpy without any issues. I am thus a bit confused about that.

I understand that I should find a way to tell Julia not to use Accelerate to precompile but I do not know how to do it. I am also surprised to be able to import numpy in a simple script (outside of that package).

I already browsed the internet a lot to find a solution but it was not successful until now. Some help would thus be really appreciated.

no, Julia doesn’t care at all, what you need to do is tell PyCall.jl which Python you want to use

this is vastly outdated syntax

From the PyCall documentation, it looks like you need to do the actual python imports inside your module’s __init__ method (which is called when the module is loaded rather than when it’s precompiled). Check out the example at the end of this section: GitHub - JuliaPy/PyCall.jl: Package to call Python functions from the Julia language

Indeed you are right! The way to call python packages is different for a simple script or for a module, which I did not suspect.

Thanks a lot!

Yeah, this is a common pitfall with modules (which are always precompiled by default). The rule of thumb I use is that it’s generally fine to do anything purely in Julia at the top level of a module, but as soon as you start interacting with another language or system (like creating C function pointers or python wrappers), you need to do your work in __init__. For more detailed information, see: Modules · The Julia Language

1 Like