有numpy没有安装就可以导入的方法吗?
我在.exePyInstaller中内置了一个通用应用程序。该应用程序具有一个插件系统,可通过Python脚本对其进行扩展。插件导入系统适用于基本模块(单个.py文件,类,函数和简单包)。在内部,它会遍历插件目录,然后使用__import__或相应地导入importlib.import_module。
该应用程序以最小的依赖关系构建,以减小可执行文件的整体大小。另外,不可能知道将来的插件将需要什么依赖关系,也不可能实际包含所有内容。但是,某些插件将不可避免地需要依赖项。numpy是解决此类问题的良好测试案例。
这是我尝试过的。
轮文件实际上只是一个目录。可以将其添加到其中sys.path并导入内容。
importsys
sys.path.append(r"C:\path\to\numpy-1.16.3+mkl-cp36-cp36m-win_amd64.whl")importnumpyasnp
读取了wheel文件,但是导入生成错误。
***ImportError:IMPORTANT:PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!Importingthe multiarray numpy extension module failed.Mostlikely you are trying toimporta failed build of numpy.Hereishow to proceed:-Ifyou'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),andthat you have no directoriesinyour PATHorPYTHONPATH that can
interferewiththePythonandnumpy 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.Originalerror was:Nomodule named'numpy.core._multiarray_umath'
令人困惑的是,wheel文件包含.pydfor_multiarray_umath。
C:\projects\zip_test\plugins\New folder\numpy\core>dirVolumeindrive CisOSVolumeSerialNumberisFE3D-6596Directoryof C:\projects\zip_test\plugins\New folder\numpy\core07/25/201902:37PM
这感觉像是一个问题。然而,添加的直接路径core/会sys.path产生相同的错误。
sys.path.append(r"C:\projects\zip_test\plugins\numpy-1.16.3+mkl-cp36-cp36m-win_amd64.whl\numpy\core")
这是怎么回事?为什么Python找不到numpy.core._multiarray_umath?
回应回复:
车轮锉刀是从Christoph Gohlke获得的。
我的操作系统是Windows 10 Pro 64位。
我在中运行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/PEDumperVersion14.22.27905.0Copyright(C)MicrosoftCorporation.Allrights reserved.Dumpof file _multiarray_umath.cp36-win_amd64.pydFileType:DLLImagehas the following dependencies:mkl_rt.dll
python36.dll
KERNEL32.dll
VCRUNTIME140.dll
api-ms-win-crt-heap-l1-1-0.dllapi-ms-win-crt-stdio-l1-1-0.dllapi-ms-win-crt-math-l1-1-0.dllapi-ms-win-crt-runtime-l1-1-0.dllapi-ms-win-crt-string-l1-1-0.dllapi-ms-win-crt-convert-l1-1-0.dllapi-ms-win-crt-time-l1-1-0.dllapi-ms-win-crt-utility-l1-1-0.dllapi-ms-win-crt-locale-l1-1-0.dllSummary77000.data1000.gfids1C000.pdata30000.rdata3000.reloc1000.rsrc1BD000.text
的依赖关系,可以找我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文件可在文件系统上访问。为此,将整个wheel文件解压缩到磁盘1可能是最简单的。
在这种情况下,请考虑“如何定义'安装'”:
If you could run numpy straight from the wheel, wouldn't that mean numpy was installed?
What difference does it really make whether you're writing a wheel file or the contents of that wheel file?
Since the plugin system is downloading a wheel file onto the filesystem, the application must have write access. Download the wheel file and extract it into the plugins directory. As long as the plugin directory is on the PYTHONPATH (i.e. sys.path.append), the import will work. Smarts and so forth can be added from there. Of course, what level of smarts you need is a whole other issue. But that's the basic idea.
The Wizard's Path
Another solution is to create your own functionality to import a dll from memory. This is precisely the goal of Joachim Bauch's MemoryModule project. It seems the Py2Exe project used the MemoryModule at one point, through a module called zipextimporter.py. Unfortunately, the code is hard to find and seems outdated (Python 2.4). There is also a similar module called pymemimporter.py which is slightly more recent.
1 Numpy expects the pyd files to be in specific locations. If you want to extract only the pyd files, they'll need to be nested in the appropriate folders (e.g. numpy/core/_multiarray_umath.cp36-win_amd64.pyd). This is sufficient to import numpy, but unless the other modules are available, rather pointless.