ImportError: Importing the multiarray numpy extension module failed - python-3.x

I am trying to write Python script in Java using ScriptEngine. But when I import tflearn module it gives me following errors.
javax.script.ScriptException: ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
Original error was: cannot import name multiarray
in <script> at line number 1
at org.python.jsr223.PyScriptEngine.scriptException(PyScriptEngine.java:222)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:59)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:31)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
at com.ultimatix.bot.JavaRunCommand.main(JavaRunCommand.java:48)
Caused by: Traceback (most recent call last):
File "<script>", line 1, in <module>
File "D:\Python\Lib\site-packages\tflearn\__init__.py", line 4, in <module>
from . import config
File "D:\Python\Lib\site-packages\tflearn\config.py", line 3, in <module>
import tensorflow as tf
File "D:\Python\Lib\site-packages\tensorflow\__init__.py", line 24, in <module>
from tensorflow.python import *
File "D:\Python\Lib\site-packages\tensorflow\python\__init__.py", line 47, in <module>
import numpy as np
File "D:\Python\Lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File "D:\Python\Lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "D:\Python\Lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "D:\Python\Lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "D:\Python\Lib\site-packages\numpy\core\__init__.py", line 26, in <module>
raise ImportError(msg)
I also tried to import some different package like sys, pickle. They are getting imported successfully.
Python version: 3.5.4
OS: Window 7 - 64 bits

Numpy is written in Fortran and compiled to binary. For that reason, it is not compatible to be ran on the JVM. The other packages you are importing are implemented in pure python.
CPython, the standard python implementation, has done a lot of work to stay compatible at the binary level. Numpy arrays have the same memory layout as C arrays, which do not have the same memory layout as Java arrays. As you go deeper into python there are a lot more incompatibilities as to why it won't work.

Related

Numpy related Error while running a docker container

I composed a docker image file that gets built successfully.
However, when I run a container with that image, it produces the following numpy error and exits.
Any idea how this could be corrected?
ERROR:
Traceback (most recent call last):
File "/opt/venv/lib/python3.9/site-packages/numpy/core/__init__.py", line 22, in <module>
from . import multiarray
File "/opt/venv/lib/python3.9/site-packages/numpy/core/multiarray.py", line 12, in <module>
from . import overrides
File "/opt/venv/lib/python3.9/site-packages/numpy/core/overrides.py", line 7, in <module>
from numpy.core._multiarray_umath import (
ImportError: Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /opt/venv/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-x86_64-linux-gnu.so)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/src/app/./MetReTrim", line 14, in <module>
import matplotlib.pyplot as plt
File "/opt/venv/lib/python3.9/site-packages/matplotlib/__init__.py", line 107, in <module>
from . import _api, cbook, docstring, rcsetup
File "/opt/venv/lib/python3.9/site-packages/matplotlib/cbook/__init__.py", line 28, in <module>
import numpy as np
File "/opt/venv/lib/python3.9/site-packages/numpy/__init__.py", line 150, in <module>
from . import core
File "/opt/venv/lib/python3.9/site-packages/numpy/core/__init__.py", line 48, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.9 from "/opt/venv/bin/python"
* The NumPy version is: "1.21.2"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /opt/venv/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-x86_64-linux-gnu.so)
Did you install numpy outside of docker, and copied the venv?
If so, C extensions were compiled for your host (which, according to the error, is not Linux)
You'll need to install numpy into the container itself

Matplotlib and numpy import don't work after pycharm 2019 optimize imports

I used Pycharm 2019.1's import optimization on a full project and now the imports for matplotlib and numpy don't work.
Here is the full error code:
Traceback (most recent call last):
File "C:/Users/LENOVO/PycharmProjects/CSinternal/LearnIt.py", line 8, in <module>
import matplotlib
File "C:\Users\LENOVO\PycharmProjects\CSinternal\venv\lib\site-packages\matplotlib\__init__.py", line 139, in <module>
from . import cbook, rcsetup
File "C:\Users\LENOVO\PycharmProjects\CSinternal\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 31, in <module>
import numpy as np
File "C:\Users\LENOVO\PycharmProjects\CSinternal\venv\lib\site-packages\numpy\__init__.py", line 141, in <module>
from . import core
File "C:\Users\LENOVO\PycharmProjects\CSinternal\venv\lib\site-packages\numpy\core\__init__.py", line 37, in <module>
from . import multiarray
File "C:\Users\LENOVO\PycharmProjects\CSinternal\venv\lib\site-packages\numpy\core\multiarray.py", line 20, in <module>
from . import overrides
File "C:\Users\LENOVO\PycharmProjects\CSinternal\venv\lib\site-packages\numpy\core\overrides.py", line 45, in <module>
""")
RuntimeError: implement_array_function method already has a docstring
I'm new here and honestly don't know what this ( RuntimeError: implement_array_function method already has a docstring) means.
Any help is much appreciated.
I had the same issue and the solution for me appeared when I realized that I had imported torch as two different names in different files in the same project. By renaming them I got rid of the error. I suspect you might have a similar issue. Try running the code in a new project without any other files in the same project.

scipy.stats attribute not found

I tried to import scipy.stats as ss but it keeps showing me an import error:
Traceback (most recent call last):
File "E:\My Python Files\KNN_Classification.py", line 4, in <module>
import scipy.stats
File "D:\Python3.6\lib\site-packages\scipy\stats\__init__.py", line 343, in <module>
from .stats import *
File "D:\Python3.6\lib\site-packages\scipy\stats\stats.py", line 169, in <module>
import scipy.special as special
File "D:\Python3.6\lib\site-packages\scipy\special\__init__.py", line 640, in <module>
from ._ufuncs import *
ImportError: DLL load failed: The specified module could not be found.
What should I do?
Most likely a wrong installation. You should remove the build directory as well as the installed scipy in site-packages, and then reinstall scipy.

Python cannot import name 'NUMPY_MKL'

I am trying to run a wavenet implementation (which uses tensorflow + librosa). However, I keep running into the error "cannot import name 'NUMPY_MKL'".
I have already installed tensorflow, numpy+mk1, scipy and librosa. Tensorflow itself is loading fine, but error is triggered when I run the wavenet implementation which uses librosa.
My spec/details:
Python 3.5.2
Numpy+MK1 (numpy-1.13.1+mkl-cp35-cp35m-win_amd64.whl)
Scipy (scipy-0.19.1-cp35-cp35m-win_amd64.whl)
Windows 10 x64
Nvidia CUDA v8.0 (cudnn 5.1)
Hope you can help. Full error log below:
Traceback (most recent call last):
File "train.py", line 20, in <module>
from wavenet import WaveNetModel, AudioReader, optimizer_factory
File "D:\musicAI\wavenet\__init__.py", line 2, in <module>
from .audio_reader import AudioReader
File "D:\musicAI\wavenet\audio_reader.py", line 7, in <module>
import librosa
File "C:\Python35\lib\site-packages\librosa\__init__.py", line 12, in <module>
from . import core
File "C:\Python35\lib\site-packages\librosa\core\__init__.py", line 108, in <module>
from .time_frequency import * # pylint: disable=wildcard-import
File "C:\Python35\lib\site-packages\librosa\core\time_frequency.py", line 10, in <module>
from ..util.exceptions import ParameterError
File "C:\Python35\lib\site-packages\librosa\util\__init__.py", line 67, in <module>
from .utils import * # pylint: disable=wildcard-import
File "C:\Python35\lib\site-packages\librosa\util\utils.py", line 5, in <module>
import scipy.ndimage
File "C:\Python35\lib\site-packages\scipy\__init__.py", line 61, in <module>
from numpy._distributor_init import NUMPY_MKL # requires numpy+mkl
ImportError: cannot import name 'NUMPY_MKL'
Managed to fixed this by doing a complete uninstall of numpy+mk1, then installing it again.
I was previously doing:
pip install --ignore-install path\numpy.whl
Do this instead to get it working:
pip uninstall numpy # this removes the existing copy
pip install path\numpy.whl # this install the numpy+mk1 from local .whl file

error of import scipy.stats for windows 7

I use windows 7 and installed python 3.5 32bit. I installed numpy 1.11 + mkl win32 whl version for python 3.5 from http://www.lfd.uci.edu/~gohlke/pythonlibs/
and installed scipy 0.17.0 from the same site same way. I use pip install. No problem import numpy and scipy, but see error when import scipy.stats or from scipy import stats, etc... Here is the error message:
if from scipy import stats:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
from scipy import stats
File "C:\Programs\Python\Python35-32\lib\site-packages\scipy\stats\__init__.py", line 338, in <module>
from .stats import *
File "C:\Programs\Python\Python35-32\lib\site-packages\scipy\stats\stats.py", line 180, in <module>
import scipy.special as special
File "C:\Programs\Python\Python35-32\lib\site-packages\scipy\special\__init__.py", line 627, in <module>
from ._ufuncs import *
File "scipy\special\_ufuncs.pyx", line 1, in init scipy.special._ufuncs (scipy\special\_ufuncs.c:26242)
ImportError: DLL load failed: The specified module could not be found.
If use import scipy.stats see below error message:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
import scipy.stats
File "C:\Programs\Python\Python35-32\lib\site-packages\scipy\stats\__init__.py", line 338, in <module>
from .stats import *
File "C:\Programs\Python\Python35-32\lib\site-packages\scipy\stats\stats.py", line 180, in <module>
import scipy.special as special
File "C:\Programs\Python\Python35-32\lib\site-packages\scipy\special\__init__.py", line 629, in <module>
from .basic import *
File "C:\Programs\Python\Python35-32\lib\site-packages\scipy\special\basic.py", line 14, in <module>
from ._ufuncs import (ellipkm1, mathieu_a, mathieu_b, iv, jv, gamma, psi, zeta,
ImportError: cannot import name 'ellipkm1'
I have searched for solution from websites but could not find an answer. Please help. This is really frustrating...
Thanks
Rocky
Ok, finally I solved this myself. At the beginning I knew I needed the 2015 visual C++ redistribution and had it installed, the problem is I installed the 64bit one while obviously I should have used 32 bit one since my python and numpy, scipy were all 32 bit.
Hope this helps for other people

Resources