Lor*_*sum 5 python numpy python-3.x
有numpy
没有安装就可以导入的方法吗?
我在.exe
PyInstaller中内置了一个通用应用程序。该应用程序具有一个插件系统,可通过Python脚本对其进行扩展。插件导入系统适用于基本模块(单个.py
文件,类,函数和简单包)。在内部,它会遍历插件目录,然后使用__import__
或相应地导入importlib.import_module
。
该应用程序以最小的依赖关系构建,以减小可执行文件的整体大小。另外,不可能知道将来的插件将需要什么依赖关系,也不可能实际包含所有内容。但是,某些插件将不可避免地需要依赖项。 numpy
是解决此类问题的良好测试案例。
这是我尝试过的。
轮文件实际上只是一个目录。可以将其添加到其中sys.path
并导入内容。
import sys
sys.path.append(r"C:\path\to\numpy-1.16.3+mkl-cp36-cp36m-win_amd64.whl")
import numpy as np
Run Code Online (Sandbox Code Playgroud)
读取了wheel文件,但是导入生成错误。
*** 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 c:\projects\zip_test\venv\Scripts\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'
Run Code Online (Sandbox Code Playgroud)
令人困惑的是,wheel文件包含.pyd
for _multiarray_umath
。
C:\projects\zip_test\plugins\New folder\numpy\core>dir
Volume in drive C is OS
Volume Serial Number is FE3D-6596
Directory of C:\projects\zip_test\plugins\New folder\numpy\core
07/25/2019 02:37 PM <DIR> .
07/25/2019 02:37 PM <DIR> ..
....
04/22/2019 02:55 AM 101,376 _multiarray_tests.cp36-win_amd64.pyd
04/22/2019 02:55 AM 2,494,976 _multiarray_umath.cp36-win_amd64.pyd
....
74 File(s) 583,173,551 bytes
5 Dir(s) 309,925,851,136 bytes free
Run Code Online (Sandbox Code Playgroud)
这感觉像是一个问题。然而,添加的直接路径core/
会sys.path
产生相同的错误。
C:\projects\zip_test\plugins\New folder\numpy\core>dir
Volume in drive C is OS
Volume Serial Number is FE3D-6596
Directory of C:\projects\zip_test\plugins\New folder\numpy\core
07/25/2019 02:37 PM <DIR> .
07/25/2019 02:37 PM <DIR> ..
....
04/22/2019 02:55 AM 101,376 _multiarray_tests.cp36-win_amd64.pyd
04/22/2019 02:55 AM 2,494,976 _multiarray_umath.cp36-win_amd64.pyd
....
74 File(s) 583,173,551 bytes
5 Dir(s) 309,925,851,136 bytes free
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?为什么Python找不到numpy.core._multiarray_umath
?
回应回复:
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
(通过Python Foundation build安装3.6.8150.0
)venv
。“系统” Python未启用PATH
。numpy-1.16.3+mkl-cp36-cp36m-win_amd64.whl
通过安装文件pip
并import numpy as np
正常工作。然后,我numpy
通过卸载pip uninstall -y numpy
。运行dumpbin /dependents _multiarray_umath.cp36-win_amd64.pyd
产生:
Microsoft (R) COFF/PE Dumper Version 14.22.27905.0
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file _multiarray_umath.cp36-win_amd64.pyd
File Type: DLL
Image has the following dependencies:
mkl_rt.dll
python36.dll
KERNEL32.dll
VCRUNTIME140.dll
api-ms-win-crt-heap-l1-1-0.dll
api-ms-win-crt-stdio-l1-1-0.dll
api-ms-win-crt-math-l1-1-0.dll
api-ms-win-crt-runtime-l1-1-0.dll
api-ms-win-crt-string-l1-1-0.dll
api-ms-win-crt-convert-l1-1-0.dll
api-ms-win-crt-time-l1-1-0.dll
api-ms-win-crt-utility-l1-1-0.dll
api-ms-win-crt-locale-l1-1-0.dll
Summary
77000 .data
1000 .gfids
1C000 .pdata
30000 .rdata
3000 .reloc
1000 .rsrc
1BD000 .text
Run Code Online (Sandbox Code Playgroud)
的依赖关系,可以找我mkl_rt.dll
,python36.dll
和VCRUNTIME140.dll
在任.whl
或venv
。显然有两个版本KERNEL32.dll
,一个用于x86,一个用于x64。其中之一居住在C:\Windows\System32
。
我api-ms-win-crt-*.dll
在MSDN上的“ Windows Universal C Runtime更新” 中描述了我能找到的唯一信息,
Windows 10通用CRT是Windows操作系统组件,可在Windows操作系统上启用CRT功能。此更新使依赖Windows 10 Universal CRT版本的Windows桌面应用程序可以在较早的Windows操作系统上运行。
这些适用于非Windows 10系统。对于Windows 10系统,似乎没有“官方”方法来获取它们。我能够从Windows 7系统复制dll。天真地将它们放入PYTHONPATH
(并不奇怪)是行不通的。如果他们要工作,则可能需要注册。但是有人告诉我1)在Windows 10上注册Windows 7 dll会使系统不稳定,2)绝对不是通用的可移植解决方案。
是的,取决于您如何定义“安装”。
Numpy 需要使用.pyd
文件。pyd 文件本质上是一个dll
. 这些是 Python 解释器在运行时引用的已编译代码文件。不幸的是,用于加载 dll 文件的 Windows API 调用要求它们存在于文件系统级别。当您尝试直接从轮子导入时,无法访问 pyd 文件(及其包含的函数/类)。因此,出现错误。
一种解决方案是使 pyd 文件在文件系统上可访问。为此,最简单的方法可能是将整个车轮文件解压到磁盘1。
在这种情况下,请考虑“如何定义‘安装’”:
由于插件系统正在将wheel文件下载到文件系统上,因此应用程序必须具有写访问权限。下载wheel文件并将其解压到插件目录中。只要插件目录位于 PYTHONPATH (即sys.path.append
)上,导入就会起作用。可以从那里添加智能等等。当然,你需要什么水平的智慧是另一个问题。但这是基本的想法。
另一种解决方案是创建您自己的功能以从内存导入 dll。这正是 Joachim Bauch 的MemoryModule 项目的目标。看来 Py2Exe 项目曾经通过一个名为 的模块使用了 MemoryModule zipextimporter.py
。不幸的是,代码很难找到并且似乎已经过时(Python 2.4)。还有一个类似的模块叫它pymemimporter.py
稍微更新一些。
1 Numpy 期望 pyd 文件位于特定位置。如果您只想提取 pyd 文件,则需要将它们嵌套在适当的文件夹中(例如numpy/core/_multiarray_umath.cp36-win_amd64.pyd
)。这足以导入 numpy,但除非其他模块可用,否则毫无意义。
归档时间: |
|
查看次数: |
183 次 |
最近记录: |