使用cx_Freeze,Python3.4导入GDAL

时间:2014-07-28 15:38:34

标签: python python-3.x cx-freeze gdal

我试图为Python3.4中的某些代码创建可执行文件,以便分发到Windows。这个程序需要GDAL来实现一些映射函数,但它在cx_Freeze构建期间出现在Missing Modules中:

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

cx_Freeze .exe仍在构建,但是当我尝试运行它时,我自然会得到:

ImportError: No module named '_gdal'

以下是我的设置脚本:

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)])

我已经尝试过各种方法在cx_Freeze安装文件的build_exe选项中手动使用includes来手动包含模块,但无法使用,并且在Python3上实际上限制了我对备用可执行分发工具的选择。有没有人想出如何解决这个导入?

1 个答案:

答案 0 :(得分:1)

我遇到了同样的麻烦,这似乎与SWIG有关。 我的解决方法是从Traceback获得所有的' osgeo'使用以下代码段抛出异常并手动修改代码的文件(例如,C:\ Python34 \ Lib \ site-packages \ osgeo__init __。py):

 except ImportError:
    import _gdal
    return _gdal

为:

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

希望它有所帮助!