[英]Importing GDAL with cx_Freeze, Python3.4
我正在尝试为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选项中的include手动包括模块,但无济于事,而在Python3上,确实限制了我对其他可执行分发工具的选择。 有人知道如何解决此导入问题吗?
我也遇到了同样的麻烦,这似乎是与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
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.