Opened 3 months ago

Closed 3 months ago

Last modified 3 months ago

#7044 closed defect (fixed)

_gdal_array import error when bundled with pyinstaller

Reported by: dhoese Owned by: hobu
Priority: normal Milestone:
Component: PythonBindings Version: unspecified
Severity: normal Keywords:
Cc:

Description

The change described in the following ticket #6801 causes an import error when bundled with the tool pyinstaller. Pyinstaller allows a user to bundle a python script and all of its dependencies in to a single directory or installer.

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    from osgeo import gdal_array
  File "/Users/davidh/repos/git/pyinstaller/PyInstaller/loader/pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages/osgeo/gdal_array.py", line 164, in <module>
ImportError: cannot import name '_gdal_array'

See https://github.com/pyinstaller/pyinstaller/issues/2829 for more details.

From what I can tell the import referenced in #6801 is redundant given the swig import helper stuff on the top of the gdal_array.py module. If I switch the import back to "import _gdal_array", my bundled pyinstaller package works. If I remove that import line, it also works. None of the other GDAL SWIG modules (_gdalconst.so, _gdal.so, _ogr.so, etc) seem to have this issue since they don't have the repeated import in the middle of the module.

I can see why the "from . import _gdal_array" is the correct way of importing something from the current subpackage (osgeo) and I'm not super familiar with standard practice with SWIG so I can understand a "no fix" on this. I do have a workaround I can implement for pyinstaller, but the only reason it seems to be needed is because gdal_array.py does its import differently than other SWIG modules.

Change History (5)

comment:1 Changed 3 months ago by dhoese

I should add that through normal use of the osgeo/gdal package (no pyinstaller) I don't get any errors with any of the 3 forms of importing (import _gdal_array, from . import _gdal_array, or no import). That's on mac with python 3.6.

Last edited 3 months ago by dhoese (previous) (diff)

comment:2 Changed 3 months ago by Even Rouault

Resolution: fixed
Status: newclosed

In 40118:

Python bindings: remove 'from . import _gdal_array' line from gdal_array.py that is not necessary with normal execution of the bindings, and cause errors with PyInstaller? (fixes #7044)

comment:3 Changed 3 months ago by Even Rouault

In 40119:

Python bindings: remove 'from . import _gdal_array' line from gdal_array.py that is not necessary with normal execution of the bindings, and cause errors with PyInstaller? (fixes #7044)

comment:4 Changed 3 months ago by Even Rouault

David, your suggestion was a good one. This line was completely useless after all. Fixed post GDAL 2.2.2RC1

comment:5 Changed 3 months ago by dhoese

Awesome. Thanks. Can't wait for the full release. I'll have to wait for conda-forge to catch up too.

Note: See TracTickets for help on using tickets.