【问题标题】:PyInstaller not able to create exe using geopandas and fiona. Even if I create with some changes in .spec file exe is not workingPyInstaller 无法使用 geopandas 和 fiona 创建 exe。即使我在 .spec 文件中进行了一些更改,exe 也无法正常工作
【发布时间】:2019-06-18 07:42:41
【问题描述】:

我正在使用简单的代码使用 pyinstaller 使用 geopandas 和 fiona 作为导入来创建 exe。

示例代码:

import glob
import geopandas as gpd
from pyproj import _datadir, datadir
import fiona
from osgeo import gdal, ogr, osr
from fiona.ogrext import Iterator, ItemsIterator, KeysIterator
from geopandas import GeoDataFrame
print("Hello")

我无法使用 PyInstaller 为该示例代码创建 exe,因为 geopandas 引起了问题。 根据此处的一篇文章,我对 .spec 文件进行了一些更改。 这让我可以使用以下 .spec 文件内容以某种方式创建 exe:

block_cipher = None

import os
from PyInstaller.utils.hooks import collect_data_files # this is very helpful
from osgeo import gdal, ogr, osr
from fiona.ogrext import Iterator, ItemsIterator, KeysIterator
from geopandas import GeoDataFrame
rTreeDlls = 'C:\\Users\\supadhayay\\AppData\\Local\\Programs\\Python\\Python37\\Lib\\site-packages\\rtree'

paths = [
    'C:\\Users\\supadhayay',
    rTreeDlls,
    'C:\\Users\\supadhayay\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
    'C:\\Users\\supadhayay\\AppData\\Local\\Programs\\Python\\Python37\\Lib\\site-packages\\osgeo'
]

_osgeo_pyds = collect_data_files('osgeo', include_py_files=True)
_osgeo_pyds  = _osgeo_pyds  + collect_data_files('fiona', include_py_files=True)

osgeo_pyds = []
for p, lib in _osgeo_pyds:
    if '.pyd' in p or '.pyx' in p or '.pyc' in p:
        osgeo_pyds.append((p, '.'))

print(osgeo_pyds)

binaries = osgeo_pyds +[
    (os.path.join(rTreeDlls,'spatialindex-64.dll'), '.'),
    (os.path.join(rTreeDlls,'spatialindex_c.dll'),'.'),
]

hidden_imports = [
    'fiona',
    'gdal',
    'shapely',
    'shapely.geometry',
    'pyproj',
    'rtree',
    'geopandas.datasets',
    'pytest',
    'pandas._libs.tslibs.timedeltas',
]


a = Analysis(['D:\\SDR\\Repo\\Main\\DBEngg\\Spatial Data Repository\\States_Data_Processing_With_Geometry\\States_Data_Processing_With_Geometry_MP.py'],
             pathex=paths,
             binaries=osgeo_pyds +[('C:\\Users\\supadhayay\\AppData\\Local\\Programs\\Python\\Python37\\Lib\\site-packages\\shapely\\DLLs\\geos_c.dll', '.'),('C:\\Users\\supadhayay\\AppData\\Local\\Programs\\Python\\Python37\\Lib\\site-packages\\rtree\\spatialindex_c.dll', '.'), ('C:\\Users\\supadhayay\\AppData\\Local\\Programs\\Python\\Python37\\Lib\\site-packages\\rtree\\spatialindex-64.dll', '.')],
             datas=collect_data_files('geopandas', subdir='datasets') + [('D:\\SDR\\Repo\\Main\\DBEngg\\Spatial Data Repository\\States_Data_Processing_With_Geometry\\lg-logo-rms.png','.'),('D:\\SDR\\Repo\\Main\\DBEngg\\Spatial Data Repository\\States_Data_Processing_With_Geometry\\SQL_States_Data_Processing.sql','.')],
             hiddenimports=hidden_imports,
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='States_Data_Processing_With_Geometry_MP',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

创建 exe 后,我无法执行 exe,因为它给出了以下错误:

文件“fiona\ogrext.pyx”,第 1 行, init fiona.ogrext ModuleNotFoundError:没有名为“fiona._shim”的模块

我使用https://www.lfd.uci.edu/~gohlke/pythonlibs 安装了fiona 版本:Fiona‑1.8.6‑cp37‑cp37m‑win_amd64.whl 我可以在站点包的 fiona 文件夹中看到 _shim 文件。 请帮忙

【问题讨论】:

  • 可以肯定的是,将安装的库放在您的项目级文件夹中。那应该至少可以解决该错误

标签: python pyinstaller geopandas shapely fiona


【解决方案1】:

我自己通过在 .spec 文件的 hidden_​​imports 中添加“fiona._shim”来修复它。

【讨论】:

  • 能否提供完成的脚本?
【解决方案2】:

可以通过将适当的参数传递给 pyinstaller 来解决此错误和类似错误。对于名为script.py 的脚本文件,您需要这样的内容:

pyinstaller --hidden-import fiona._shim --hidden-import fiona.schema script.py

哪些包需要--hidden-import处理可以直接从ModuleNotFoundError读取。另请参阅 PyInstaller 文档的 this portion

(我认为这就是@suryakant-upadhayay 他的自我回答的意思,但我还使用一些可能与 geopandas 无关的附加标志为我自己的项目构建了一个工作案例。)

一般情况下,您还可以添加community-defined "hooks",这将使 PyInstaller 成为更好的复杂包。例如,请参阅this pull request

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-28
    • 2019-03-14
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    相关资源
    最近更新 更多