Search code examples
python-3.xpyinstallergdal

pyinstaller executable fails


I need to create an executable file from my Python 3 application. Because I need to do it in Linux (Ubuntu) and Windows, I decided to use PyInstaller (it allows to create a single executable file, supports multiple platforms and works with Python 3). This application uses GDAL to translate and warp images. When I run it with activated conda environment it works just fine.

Here is my spec file I generated with pyi-makespec:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['../../src/app.py'],
            pathex=['var/pyinstaller/'],
            binaries=[],
            datas=[],
            hiddenimports=[],
            hookspath=[],
            runtime_hooks=[],
            excludes=[],
            win_no_prefer_redirects=False,
            win_private_assemblies=False,
            cipher=block_cipher,
            noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
            cipher=block_cipher)
exe = EXE(pyz,
        a.scripts,
        a.binaries,
        a.zipfiles,
        a.datas,
        [],
        name='app',
        debug=False,
        bootloader_ignore_signals=False,
        strip=False,
        upx=True,
        runtime_tmpdir=None,
        console=True )

And here is the error it keeps throwing:

ERROR 1: PROJ: proj_create_from_database: Open of /home/cisu/anaconda3/envs/env_name/share/proj failed
ERROR 1: Translating source or target SRS failed:
EPSG:5678
ERROR 1: PROJ: proj_create_from_wkt: Open of /home/cisu/anaconda3/envs/env_name/share/proj failed
ERROR 1: PROJ: pj_obj_create: Open of /home/cisu/anaconda3/envs/env_name/share/proj failed
Traceback (most recent call last):
File "src/app.py", line 235, in <module>
File "src/app.py", line 204, in process
File "src/app.py", line 165, in _warp
File "site-packages/osgeo/gdal.py", line 625, in Warp
File "site-packages/osgeo/gdal.py", line 3410, in wrapper_GDALWarpDestName
TypeError: in method 'wrapper_GDALWarpDestName', argument 4 of type 'GDALWarpAppOptions *'
[17879] Failed to execute script app

This is for --onedir configuration and it behaves exactly the same for the --onefile configuration. GDAL is not installed in my system, but it is installed in the conda environment I use to build the binary file. Does anyone have an idea on what am I doing wrong?

EDIT

I started the binary file in the fresh Ubuntu installation and that's what I've got:

ERROR 1: PROJ: proj_create_from_database: Cannot find proj.db
ERROR 1: Translating source or target SRS failed:
EPSG:5678

For some reason the error is different, but it's still not working.


Solution

  • I found the solution to this issue. I copied the file proj.db from anaconda environment to the directory where the executable file is and then I added a runtime hook to the spec file to set the PROJ_LIB directory to the current location of the executable file. This is the line from the spec file:

    runtime_hooks=['hook.py']
    

    and here is the whole hook.py:

    import os
    import sys
    
    os.environ['PROJ_LIB'] = os.path.dirname(sys.argv[0])
    

    It solves the problem and the application is working as long as the proj.db file exists at the location of the executable file.