Importing GDAL with cx_Freeze, Python3.4

六月ゝ 毕业季﹏ 提交于 2019-12-23 05:41:58

问题


I'm trying to create an executable for some code in Python3.4, for distribution to Windows. This program requires GDAL for some mapping functions, but it comes up in the Missing Modules during the cx_Freeze build:

Missing modules:
? _gdal imported from osgeo, osgeo.gdal
? _gdal_array imported from osgeo.gdal_array
? _gdalconst imported from osgeo.gdalconst
? _ogr imported from osgeo.ogr
? _osr imported from osgeo.osr

The cx_Freeze .exe still builds, but when I try to run it, I naturally get:

ImportError: No module named '_gdal'

Below is my setup script:

import sys  
from cx_Freeze import setup, Executable 

application_title = "Parametric_Price" #what you want to application to be called
main_python_file = "ParamMain.py" #the name of the python file you use to run the program

base = None
if sys.platform == "win32":
    base = "Win32GUI"

includes = ["atexit","re","osgeo.ogr"]
packages = ["osgeo"]
# includeFiles = 

build_exe_options = {"packages": packages, "includes": includes}

setup(
        name = application_title,
        version = "0.1",
        description = "Parametric pricing tool using historical earthquake, hurricane datasets",
        options = {"build_exe" : build_exe_options },
        executables = [Executable(main_python_file, base = base)])

I've tried various ways of including the module manually using includes in the build_exe options in the cx_Freeze setup file, to no avail, and being on Python3 really limits my options for alternate executable distribution tools. Has anyone figured out how to resolve this import?


回答1:


I have got the same troubles, it seems to be a SWIG-related issue. My workaround was to get from the Traceback all the 'osgeo' files that throw exception and manually modify the code (e.g., C:\Python34\Lib\site-packages\osgeo__init__.py) with the following snippet:

 except ImportError:
    import _gdal
    return _gdal

to:

 except ImportError:
    from osgeo import _gdal # MANUAL PATCH: added 'from osgeo'
    return _gdal

Hope it helps!



来源:https://stackoverflow.com/questions/24998862/importing-gdal-with-cx-freeze-python3-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!