Pyinstaller - GDAL call from os.system (gdal_translate)

Greetings, I recognized the fellows. Running 32-bit Python2.7 on Windows 7.

I have a question regarding the inclusion of GDAL executables in the pyinstaller assembly. I am making a system call to run two GDAL functions from the FWTools release. These functions are located in the PATH variable in the windows C:\Program Files (x86)\FWTools2.4.7\bin , and therefore it works fine in the Python27 environment. However, this path does not migrate to the pyinstaller assembly.

This code calls the GDAL function to re-translate the image to different geospatial coordinates.

 os.system("gdal_translate -of GTiff -a_ullr 694440.7939 6403967.2406 696438.7261 6404791.6774 -a_srs EPSG:28355 site.tif siteGR.tif") os.system("gdalinfo siteGR.tif") 

Runtime works well until it falls into the above lines, and then returns the following error:

 'gdal_translate' is not recognized as an internal or external command, operable program or batch file. 'gdalinfo' is not recognized as an internal or external command, operable program or batch file. 

I tried to include both gdal_translate.exe and gdalinfo.exe in the build folder as binaries, just as you would with .dll, but since it does not work after the script started working, I think this applies to them.

I have included the specification file below. I could use some tips on how to get the pyinstaller assembly to recognize executable files running from the system in a python script.

Spec:

 # -*- mode: python -*- a = Analysis(['gis_helper.py'], pathex=['C:\\Users\\Hp\\PycharmProjects\\GISdev'], hiddenimports=['scipy.linalg.cython_blas', 'scipy.linalg.cython_lapack', 'scipy.special._ufuncs_cxx', 'ctypes.util', 'pandas.util', 'distutils.util', 'shapely', '_socket', '_proj', 'multiprocessing', '_multiprocessing', 'multiprocessing.process', 'multiprocessing.util'], hookspath=['C:\\Python27\\Lib\\site-packages\\PyInstaller\\hooks'], runtime_hooks=None) a.binaries1=['geos_c.dll', 'geos_c.dll', 'BINARY'], a.binaries2=['python27.dll', 'python27.dll', 'BINARY'], a.binaries3=['_socket.pyd', '_socket.pyd', 'BINARY'], a.binaries4=['win32api.pyd', 'win32api.pyd', 'BINARY'], a.binaries5=['pywintypes27.dll', 'pywintypes27.dll', 'BINARY'], a.binaries6=['pythoncom27.dll', 'pythoncom27.dll', 'BINARY'], a.binaries7=['_imaging.pyd', '_imaging.pyd', 'BINARY'], a.binaries8=['_fblas.pyd', '_fblas.pyd', 'BINARY'], a.binaries9=['gdal_translate.exe', 'gdal_translate.exe', 'BINARY'], a.binaries10=['gdalinfo.exe', 'gdalinfo.exe', 'BINARY'], import mpl_toolkits.basemap import os src_basedata = os.path.join(mpl_toolkits.basemap.__path__[0], "data") tgt_basedata = os.path.join('mpl_toolkits', 'basemap', 'data') pyz = PYZ(a.pure) exe = EXE(pyz, a.scripts, [('v',None,'OPTION')], a.binaries, # This needs to be included a.binaries1, a.binaries2, a.binaries3, a.binaries4, a.binaries5, a.binaries6, a.binaries7, a.binaries8, a.binaries9, a.binaries10, a.zipfiles, a.datas + Tree(src_basedata, prefix=tgt_basedata), name='gis_helper.exe', debug=True, strip=None, upx=True, console=True ) 
+2
source share
1 answer

From GDAL 2.1, you can use the "liberated" version of gdal_translate in python bindings, which can be more reliable and prevent you from guessing using the path:

 import gdal gdal.Translate(<options>) 

Some examples of its use can be found in the gdal repository test suite: https://svn.osgeo.org/gdal/trunk/autotest/utilities/test_gdal_translate_lib.py

You can pip install gdal and I believe this is release 2.1 (on Linux). For windows, the easiest installation method is conda .

This should prevent you from getting confused, including too many binaries / paths in pyinstaller, as this can be considered like any python module

0
source

All Articles