python - "Import Error: DLL load failed"同时使用 pyinstaller 将 ".py file"(具有 "import rasterio")转换为 ".exe file"

标签 python pyinstaller rasterio

我有一个名为 test.py 的 python(版本 3.7)文件,我想使用 将其转换为 test.exe pyinstaller。当我使用命令时

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 文件。我建议您按照here中的安装过程进行操作并确保您已在 conda 环境中正确安装了 rasterio(为此使用新的环境)。

接下来,检查 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

关于python - "Import Error: DLL load failed"同时使用 pyinstaller 将 ".py file"(具有 "import rasterio")转换为 ".exe file",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56881955/

相关文章:

python - 生成 GeoTIFF 颜色图

python - 如何运行文本文件中包含的可执行文件(exe 的十六进制)

python - 如何将我的登录详细信息传递到网页?

python - 为什么我的 CreateAPIView 不显示产品和商店类别字段?

python - "AttributeError: ' _NotSet ' object has no attribute ' lower '"使用 Pyinstaller 将 PRAW python 文件转换为 exe 时

python - 等值线图如何与 Python 中的阴影栅格相结合?

Python3.7 rasterio库打不开jp2

python - 在列表中查找表达式的答案

python-3.x - 使用 pyinstaller 打包

pyinstaller EXE OSError : could not find or load spatialindex_c-64. dll