# @pyimport numpy throwing an error when precompiling a module

**URL:** <https://discourse.julialang.org/t/pyimport-numpy-throwing-an-error-when-precompiling-a-module/86599>\
**Category:** General Usage\
**Tags:** pycall\
**Created:** [August 31, 2022, 1:36pm UTC](https://discourse.julialang.org/t/pyimport-numpy-throwing-an-error-when-precompiling-a-module/86599 "2022-08-31T13:36:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcrismer](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jcrismer](https://discourse.julialang.org/u/jcrismer)\
**Post date:** [August 31, 2022, 1:36pm UTC](https://discourse.julialang.org/t/pyimport-numpy-throwing-an-error-when-precompiling-a-module/86599/1 "2022-08-31T13:36:24Z")

</div>

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

```julia
@pyimport numpy as np

```

throw me the error

```julia
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.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 31, 2022, 1:42pm UTC](https://discourse.julialang.org/t/pyimport-numpy-throwing-an-error-when-precompiling-a-module/86599/2 "2022-08-31T13:42:29Z")

</div>

> [@jcrismer](#):
>
> I understand that I should find a way to tell Julia not using Accelerate to precompile but I

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

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 31, 2022, 1:42pm UTC](https://discourse.julialang.org/t/pyimport-numpy-throwing-an-error-when-precompiling-a-module/86599/3 "2022-08-31T13:42:44Z")

</div>

> [@jcrismer](#):
>
> `@pyimport numpy as np`

this is vastly outdated syntax

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 31, 2022, 1:47pm UTC](https://discourse.julialang.org/t/pyimport-numpy-throwing-an-error-when-precompiling-a-module/86599/4 "2022-08-31T13:47:10Z")

</div>

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](https://github.com/JuliaPy/PyCall.jl#usage)

---

<div class="post-metadata">

**Author:** ![jcrismer](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jcrismer](https://discourse.julialang.org/u/jcrismer)\
**Post date:** [August 31, 2022, 2:44pm UTC](https://discourse.julialang.org/t/pyimport-numpy-throwing-an-error-when-precompiling-a-module/86599/5 "2022-08-31T14:44:00Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 31, 2022, 6:43pm UTC](https://discourse.julialang.org/t/pyimport-numpy-throwing-an-error-when-precompiling-a-module/86599/6 "2022-08-31T18:43:49Z")

</div>

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](https://docs.julialang.org/en/v1/manual/modules/#Module-initialization-and-precompilation)
