![](/img/trans.png)
[英]Import error: DLL load failed in Jupyter notebook but working in .py file
[英]"Import Error: DLL load failed" while converting ".py file" (having "import rasterio") to ".exe file" using pyinstaller
我有一个名为test.py
的 python( 3.7
版)文件,我想使用pyinstaller
将其转换为test.exe
。 当我使用命令时
pyinstaller test.py
它正在成功创建test.exe
。 但是当我尝试使用命令提示符执行test.exe
文件时,出现以下错误:
"Traceback (most recent call last):
File "test.py", line 1, in <module>
File "c:\users\user1\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\rasterio\__init__.py", line 29, in <module>
ImportError: DLL load failed: The specified module could not be found.
[460] Failed to execute script test"
在网站上浏览了类似的帖子后,我尝试了不同的选项,例如:
(i) 第一个选项:在路径C:\\Users\\user1\\Anaconda3\\Lib\\site-packages\\PyInstaller\\hooks
我添加了一个hook-rasterio.py
包含hiddenimports=['rasterio', 'rasterio._shim']
然后尝试
pyinstaller -F test.py
但我仍然收到上述错误。
(ii) 第二个选项:在hiddenimports=[]
的test.spec
文件中,我添加了rasterio
和rasterio._shim
,然后使用pyinstaller
创建了test.exe
但问题仍然存在。
我的test.py
看起来像:
import rasterio
print("It's Done....")
任何人都可以建议可以解决问题的必要措施。
rasterio
是一个复杂的库,它依赖于许多外部库。 您的错误是 DLL 加载错误,这意味着它缺少rasterio
所需的一些 DLL 文件。 我建议您按照此处的安装过程进行操作,并确保在rasterio
环境中正确安装了rasterio
(为此使用新的 env)。
接下来,检查rasterio
是否导入没有任何问题,例如:
import traceback
try:
import rasterio
print("Import OK!")
except ImportError:
print("Import Error!")
traceback.print_exc()
input()
接下来,安装 PyInstaller 并使用以下规范文件:
# -*- mode: python -*-
block_cipher = None
a = Analysis(['test.py'],
pathex=['C:\\Users\\Masoud\\Desktop\\test'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
a.datas += Tree('C:\\Users\\Masoud\\Anaconda3\\envs\\testEnv\\Lib\\site-packages\\rasterio\\', prefix='rasterio')
a.datas += Tree('C:\\Users\\Masoud\\Anaconda3\\envs\\testEnv\\Lib\\xml', prefix='xml')
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='test',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=False,
runtime_tmpdir=None,
console=True )
在上面的脚本中,我将整个rasterio
和xml
库放在可执行文件旁边,因为 PyInstaller 无法解析模块导入。 请记住根据您的设置更改路径。
最后,使用以下命令生成可执行文件:
pyinstaller test.spec
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.