繁体   English   中英

使用 pyinstaller 将“.py 文件”(具有“导入光栅”)转换为“.exe 文件”时出现“导入错误:DLL 加载失败”

[英]"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文件中,我添加了rasteriorasterio._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 )

在上面的脚本中,我将整个rasterioxml库放在可执行文件旁边,因为 PyInstaller 无法解析模块导入。 请记住根据您的设置更改路径。

最后,使用以下命令生成可执行文件:

pyinstaller test.spec

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM