使用cx_Freeze导入GDAL,Python3.4
我正在尝试为一些Python3.4的代码创建一个可执行文件,以便在Windows上分发。这个程序需要GDAL来实现一些地图功能,但在使用cx_Freeze构建时,它在缺失模块中出现了问题:
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选项手动包含这个模块,但都没有成功。而且因为我使用的是Python3,这让我在寻找其他可执行文件分发工具时选择很有限。有没有人找到解决这个导入问题的方法?
1 个回答
1
我也遇到了同样的问题,这看起来是和SWIG有关的。
我的解决办法是从错误信息中找到所有抛出异常的'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
希望这能帮到你!