我使用pip install numpy
在我的Mac上安装了numpy
在此之后,我从site-packages
获取了已安装的软件包并创建了一个名为numpy.zip
的zip存档
我尝试使用zip导入numpy,如下所示:
>>> import sys
>>> sys.path.insert(0,'numpy.zip')
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "numpy/__init__.py", line 142, in <module>
File "numpy/add_newdocs.py", line 13, in <module>
File "numpy/lib/__init__.py", line 8, in <module>
File "numpy/lib/type_check.py", line 11, in <module>
File "numpy/core/__init__.py", line 24, in <module>
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.
我检查了numpy/core/
目录,我看到其中有multiarray.so
个文件。
这种技术适用于其他软件包,但不知何故因为numpy而失败。 我还想指出导入解压缩的numpy模块是成功的。
答案 0 :(得分:0)
我也遇到了这个问题,如果您手头有一个将使用压缩依赖项的程序,PyInstaller可能是解决此问题的一种方法。
当前的主要问题是zip包含共享库(* .so文件),这些文件不在系统查找共享库的任何地方。即使您指定LD_LIBRARY_PATH=<path to zip file>/numpy.zip
,系统仍将找不到共享库。为此,您必须解压缩zip文件,然后可以指定LD_LIBRARY_PATH来解压缩zip文件。如果这恰好是您的当前工作目录,那么您很可能不必将当前工作目录添加到LD_LIBRARY_PATH中,因为系统很可能仍在搜索当前工作目录。
就我而言,这比较棘手,因为我需要将依赖项包含在Spark / Zeppelin设置中,在该设置中我没有手边的程序,PyInstaller可以分析该程序。我不能包含任何主程序,因为那是Zeppelin笔记本服务器的用途。
我的案例可以复制如下,以numpy为例。考虑numpy.dockerfile:
FROM debian:stable-slim as base
RUN apt update && \
apt install -y \
zip \
python3 \
python3-pip
# Make sure we have the latest tools
RUN pip3 install -U pip
RUN pip3 install -U wheel setuptools
FROM base as pkgbuilder
RUN mkdir pkg pkg1
WORKDIR /pkg
RUN pip3 install numpy -t .
RUN zip -r9 /pkg.zip * -x *.pyc
FROM base
RUN mkdir test
COPY --from=pkgbuilder /pkg.zip /test/
这样构建它:docker build -f numpy.dockerfile -t numpy .
。现在,您有一个标记为numpy:latest的图像。现在我们使用该图像docker run -it -e PYTHONPATH=/test/pkg.zip numpy python3 -c 'import numpy'
。
作为输出我得到
Traceback (most recent call last):
File "/test/pkg.zip/numpy/core/__init__.py", line 22, in <module>
File "/test/pkg.zip/numpy/core/multiarray.py", line 12, in <module>
File "/test/pkg.zip/numpy/core/overrides.py", line 7, in <module>
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/test/pkg.zip/numpy/__init__.py", line 140, in <module>
File "/test/pkg.zip/numpy/core/__init__.py", line 48, in <module>
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.7 from "/usr/bin/python3"
* The NumPy version is: "1.19.2"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: No module named 'numpy.core._multiarray_umath'
由于系统不会查看共享库的zip文件,因此我无法解决此问题。