使用cx_Freeze导入GDAL,Python3.4
我正在尝试为Python3.4中的一些代码创建一个可执行文件,以便分发到Windows。该程序需要GDAL来实现某些映射功能,但在cx_冻结构建过程中,它出现在缺少的模块中:使用cx_Freeze导入GDAL,Python3.4,python,python-3.x,cx-freeze,gdal,Python,Python 3.x,Cx Freeze,Gdal,我正在尝试为Python3.4中的一些代码创建一个可执行文件,以便分发到Windows。该程序需要GDAL来实现某些映射功能,但在cx_冻结构建过程中,它出现在缺少的模块中: Missing modules: ? _gdal imported from osgeo, osgeo.gdal ? _gdal_array imported from osgeo.gdal_array ? _gdalconst imported from osgeo.gdalconst ? _ogr imported f
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\u Freeze安装文件的build\u exe选项中使用includes手动包含模块,但都没有效果,而且使用Python3确实限制了我对备用可执行分发工具的选择。有人知道如何解决此导入吗?我也遇到了同样的问题,这似乎是一个与SWIG相关的问题。 我的解决方法是从回溯引发异常的所有“osgeo”文件中获取,并使用以下代码片段手动修改代码(例如,C:\Python34\Lib\site packages\osgeo\uuuuu init\uuuu.py):
except ImportError:
import _gdal
return _gdal
致:
希望有帮助 如果您在Python shell中导入_gdal,然后打印_gdal,您会看到什么?@ThomasK It导入时不会出错,我可以访问它的功能。在程序本身中,我通过osgeo.ogr、osgeo.osr间接导入它,它在那里也能正常工作。我相信这个问题与我的cx\U冻结设置有关。我理解。但是,
print\u gdal
应该会告诉你它是从哪里加载的,这可能会给你一个线索,解释为什么cx\u Freeze找不到它。
except ImportError:
from osgeo import _gdal # MANUAL PATCH: added 'from osgeo'
return _gdal