I am trying to freeze a Python script that contains an import from osgeo
but the executable fails with an ImportError: No module named '_gdal'
.
I've stripped down my script to just one line:
import osgeo
When running in Python (version 3.3.3) all is well. I've tried freezing with cx_Freeze and py2exe. Both report that there is a missing module: ? _gdal imported from osgeo
(among others) but successfully freeze the script. Then the exe fails with the above ImportError.
I've tried importing _gdal
in python and it works. I tried manually including the osgeo
module in the freezing options but still get the same error.
There is a similar question here: Importing GDAL with cx_Freeze, Python3.4 but maybe this isn't a cx_freeze issue because it also happens with py2exe (it has experimental support for python 3 now).
Does anybody know how to fix this?
I found a fix. I edited the osgeo\__init__.py
file and modified line 13 like this: import osgeo._gdal
. This works but only if the module osgeo._gdal is manually included when freezing. @Thomas K Your solution does the same thing I guess.
Note that the same modification must be applied both at osgeo.ogr
(line 18) and osgeo.osr
(line 18) modules if they are needed (import osgeo._ogr
and import osgeo._osr
respectively). This means that these must be manually included when freezing as well. My freezing command now looks like this: python cxfreeze.py module1.py --include-modules=osgeo._gdal,osgeo._ogr,osgeo._osr
.
Thanks @Thomas K for your help.