python - 将 numpy 嵌入到 zipapp 存档中
问题描述
我想压缩一些来源,以便从多个位置轻松使用它。我必须安装一些外部模块,让我们说pyyaml和numpy。
首先,我将这些包安装在特定位置:
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 吗?
解决方案
我相信这是 zipapp 在导入一些 .c 编译文件时的问题。为了解决这个问题,我使用了install creator。这就是我想要的,从 python 代码创建一个 .exe。
推荐阅读
- ghostscript - 如何从 Windows 10 的命令行访问 Ghotscript?
- javascript - 用于 nuxt.js 的 Vuex LocaleStorage 插件
- python - 我可以使用“with open”命令在 Python 3 中打开 CSV URL 吗?
- python - Anaconda/Spyder (Python 3.7) 显示 AttributeError:“HTMLParserTreeBuilder”对象没有属性“store_line_numbers”。熊猫有问题吗?
- uml - 在用例图中应该将什么建模为子用例?
- javascript - Javascript 代码仅在空缓存和硬重新加载时有效
- c++ - 在全局范围内扩展宏时插入无序映射
- spring-boot - spring boot 关于敏感配置文件,自动化打包管理
- reactjs - 在 react app 中使用 axios 向服务器发送文件
- reactjs - 有没有办法告诉 lerna/npm '真正在我自己的 node_modules 中查找那些对等依赖项'?