简体   繁体   English

使用cx_Freeze,Python3.4导入GDAL

[英]Importing GDAL with cx_Freeze, Python3.4

I'm trying to create an executable for some code in Python3.4, for distribution to Windows. 我正在尝试为Python3.4中的某些代码创建可执行文件,以分发到Windows。 This program requires GDAL for some mapping functions, but it comes up in the Missing Modules during the cx_Freeze build: 该程序需要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

The cx_Freeze .exe still builds, but when I try to run it, I naturally get: cx_Freeze .exe仍会生成,但是当我尝试运行它时,我自然会得到:

ImportError: No module named '_gdal'

Below is my setup script: 以下是我的安装脚本:

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

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. 我尝试了多种方法,包括使用cx_Freeze设置文件中的build_exe选项中的include手动包括模块,但无济于事,而在Python3上,确实限制了我对其他可执行分发工具的选择。 Has anyone figured out how to resolve this import? 有人知道如何解决此导入问题吗?

I have got the same troubles, it seems to be a SWIG-related issue. 我也遇到了同样的麻烦,这似乎是与SWIG相关的问题。 My workaround was to get from the Traceback all the 'osgeo' files that throw exception and manually modify the code (eg, C:\\Python34\\Lib\\site-packages\\osgeo__init__.py) with the following snippet: 我的解决方法是从Traceback中获取引发异常的所有“ osgeo”文件,并使用以下代码片段手动修改代码(例如C:\\ Python34 \\ Lib \\ site-packages \\ osgeo__init __。py):

 except ImportError:
    import _gdal
    return _gdal

to: 至:

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

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM