[GIS] working standalone app .exe from python3 script that imports osgeo gdal

anacondagdalgeotiff-tiffpy2exepython 3

I have a script written in python 3.5 that uses osgeo gdal (1.11) to extract maps and file info from geotiff files.
I would like to compile this code to create a standalone app on windows, and I use pyinstaller (3.2) for this.

The code compiles but does not run, i get the following runtime error:

File "site-packages\osgeo\__init__.py", line 15, in swig_import_helper
File "imp.py", line 296, in find_module    
ImportError: No module named '_gdal' 

this is from compiling test1.py, which has a single line:

from osgeo import gdal

with

pyinstaller.exe --onefile --debug test1.py

and running the resulting test1.exe.

[other tests, not importing gdal, compile and run fine].

It seems there are problems importing osgeo libraries when compiling using pyinstaller and py2exe.

I have seen this question: Working Windows Standalone .EXE From QGIS Python Script Using PyInstaller?
and i guess it is related but because it is specific to a qgis environment i do not know how to adapt the solution to my problem.

I have also seen there is a pyinstaller hook for osgeo libraries:
https://github.com/pyinstaller/pyinstaller/issues/1522, and
https://trac.osgeo.org/gdal/ticket/6364,
and i checked i've got this hook in my pyinstaller folder, but it does not solve the import problem for me.

I have also posted to the pyinstaller mailing list but obtained no answer.

How can I obtain a working executable from a standard python script which imports GDAL ?

Best Answer

I encountered the same issue for a script importing ogr, and was able to compile a working exe by adding the path to GDAL in the pyinstaller command:

C:\Python36\Scripts\pyinstaller.exe --onefile --windowed -p C:\Python36\Lib\site-packages\osgeo python_script.pyw

This is under Python 3.6.1, with GDAL 2.1.3 (all 64-bit), with pyinstaller 3.4.