问题描述
我正在尝试为Python3.4中的某些代码创建可执行文件,以分发到Windows。该程序需要GDAL才能使用某些映射功能,但是它会在cx_Freeze构建期间出现在Missing Modules中:
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
cx_Freeze .exe仍会生成,但是当我尝试运行它时,我自然会得到:
The cx_Freeze .exe still builds, but when I try to run it, I naturally get:
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上,这实际上限制了我对其他可执行分发工具的选择。
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?
推荐答案
我也遇到了同样的麻烦,似乎与SWIG相关问题。
我的解决方法是从Traceback中获取引发异常的所有 osgeo文件,并使用以下命令手动修改代码(例如C:\Python34\Lib\site-packages\osgeo__init __。py)。以下代码段:
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
至:
except ImportError:
from osgeo import _gdal # MANUAL PATCH: added 'from osgeo'
return _gdal
希望有帮助!
这篇关于使用cx_Freeze,Python3.4导入GDAL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!