Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 965
Mateusz Cisek Avatar asked Sep 19 '26 17:09

Mateusz Cisek


2 Answers

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.

like image 116
Mateusz Cisek Avatar answered Sep 21 '26 07:09

Mateusz Cisek


Maybe a bit late, but I hope this could be of help for someone else. I had a similar issue and got the same error message:

ERROR 1: PROJ: proj_create_from_wkt: Open of /home/user/miniconda/envs/test_env/share/proj failed

, but with Python 2.7. What conda channel and what GDAL version did you specify? Maybe you can solve your issue similar as I did, without resetting the environmental variable. At the moment, conda installs the version 3.0.0 from the conda-forge channel by default. I solved it by using an older version of GDAL:

conda install -c conda-forge gdal=2.4.1
like image 43
clax Avatar answered Sep 21 '26 08:09

clax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!