简体   繁体   中英

Importing GDAL with cx_Freeze, Python3.4

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

The cx_Freeze .exe still builds, but when I try to run it, I naturally get:

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. Has anyone figured out how to resolve this import?

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 (eg, C:\\Python34\\Lib\\site-packages\\osgeo__init__.py) with the following snippet:

 except ImportError:
    import _gdal
    return _gdal

to:

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

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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