我正在使用简单的代码使用 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: No module named 'fiona._shim'
我使用https://www.lfd.uci.edu/~gohlke/pythonlibs版本安装了 fiona :Fiona‑1.8.6‑cp37‑cp37m‑win_amd64.whl 我可以在站点包的 fiona 文件夹中看到 _shim 文件。请帮忙