You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

打包为可执行文件后代码运行失败:Fiona CRS错误原因排查

解决PyInstaller打包Geopandas代码后CRSError: Invalid input to create CRS的问题

本地PyCharm中运行正常的Geopandas处理代码,用pyinstaller --onefile打包为单文件可执行程序后,运行抛出错误:

fiona.errors.CRSError: Invalid input to create CRS: epsg:32012

核心原因

PyInstaller打包时未自动包含Proj依赖的EPSG坐标参考系数据库文件,而Fiona/Geopandas在将EPSG代码转换为WKT时需要读取该数据库。开发环境下系统或虚拟环境已包含该数据库,因此运行正常。

解决方案

方案1:打包时包含Proj数据库并指定路径

  1. 定位Proj数据库目录:
    在你的Python虚拟环境或系统Python路径中找到proj目录,通常路径为:

    • 虚拟环境:venv/Lib/site-packages/pyproj/proj_dir/share/proj
    • 系统Python:C:\PythonXX\Lib\site-packages\pyproj\proj_dir\share\proj(Windows)或/usr/lib/pythonX.X/site-packages/pyproj/proj_dir/share/proj(Linux/macOS)
  2. 修改代码添加环境变量:
    在代码开头添加以下逻辑,打包后指定Proj数据库的加载路径:

    import os
    import sys
    
    if getattr(sys, 'frozen', False):
        # 打包后的运行环境,指向打包时加入的proj目录
        proj_lib_path = os.path.join(sys._MEIPASS, 'proj')
        os.environ['PROJ_LIB'] = proj_lib_path
    
  3. 用PyInstaller命令打包:
    使用--add-data参数将Proj数据库目录打包进程序,Windows用分号分隔路径,Linux/macOS用冒号:

    # Windows
    pyinstaller --onefile --add-data "C:\path\to\proj;proj" Shapefile_Tool.py
    # Linux/macOS
    pyinstaller --onefile --add-data "/path/to/proj:proj" Shapefile_Tool.py
    

方案2:直接使用WKT字符串替代EPSG代码

跳过EPSG数据库查询,直接将EPSG:32012对应的WKT字符串设置给GeoDataFrame,避免依赖外部数据库:

# EPSG:32012对应的WKT字符串
wkt_crs = 'PROJCS["NAD83 / Texas South Central",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["latitude_of_origin",27.83333333333333],PARAMETER["central_meridian",-99],PARAMETER["standard_parallel_1",28.38333333333333],PARAMETER["standard_parallel_2",30.28333333333333],PARAMETER["false_easting",2000000],PARAMETER["false_northing",0],UNIT["US survey foot",0.3048006096012192,AUTHORITY["EPSG","9003"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32012"]]'

# 设置GeoDataFrame的CRS
shapefile.crs = wkt_crs

方案3:通过Spec文件配置打包

如果使用PyInstaller的spec文件进行打包,在spec文件的datas列表中添加Proj数据库的路径映射:

# Shapefile_Tool.spec
a = Analysis(
    ['Shapefile_Tool.py'],
    datas=[('C:\\path\\to\\proj', 'proj')],  # Windows路径,Linux/macOS替换为对应路径
    # 其他配置项...
)
# 其余spec内容...

然后运行打包命令:

pyinstaller Shapefile_Tool.spec

内容的提问来源于stack exchange,提问作者Adam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 13:54:53