首页 > 解决方案 > 将 numpy 嵌入到 zipapp 存档中

问题描述

我想压缩一些来源,以便从多个位置轻松使用它。我必须安装一些外部模块,让我们说pyyamlnumpy

首先,我将这些包安装在特定位置:

python -m pip install --target=lib pyyaml numpy

然后我创建压缩库:

python -m zipapp library --output=my_library.pyd --compress

在另一个解释器中,在将“my_library.pyd”添加到 之后sys.path,我可以加载yaml模块,但不能加载numpy

>>> import sys
>>> from pathlib import Path
>>> sys.path.append(str(Path().absolute() / 'my_library.pyd'))
>>> import yaml
>>> import numpy
Traceback (most recent call last):
  File "D:\Documents\rdpy\packaging_tests\my_library.pyd\numpy\core\__init__.py", line 40, in <module>
  File "D:\Documents\rdpy\packaging_tests\my_library.pyd\numpy\core\multiarray.py", line 12, in <module>
  File "D:\Documents\rdpy\packaging_tests\my_library.pyd\numpy\core\overrides.py", line 6, 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 "<stdin>", line 1, in <module>
  File "D:\Documents\rdpy\packaging_tests\my_library.pyd\numpy\__init__.py", line 142, in <module>
  File "D:\Documents\rdpy\packaging_tests\my_library.pyd\numpy\core\__init__.py", line 71, in <module>
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
  your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
  1. Check that you are using the Python you expect (you're using D:\Apps\python-3.7\python.exe),
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy versions you're trying to use.
  2. If (1) looks fine, you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on:
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed
     - if you built from source, your compiler versions and ideally a build log

     Note: this error has many possible causes, so please don't comment on
     an existing issue about this - open a new one instead.

Original error was: No module named 'numpy.core._multiarray_umath'

在 zip 存档中,我确实有一个名为 的文件numpy\core\_multiarray_umath.cp37-win_amd64.pyd,但它显然是一个 C 编译文件。

在创建 zip 存档时,我能做些什么来导入 numpy 吗?

标签: pythonpython-3.xnumpy

解决方案


我相信这是 zipapp 在导入一些 .c 编译文件时的问题。为了解决这个问题,我使用了install creator。这就是我想要的,从 python 代码创建一个 .exe。


推荐阅读