Opened 7 years ago

Closed 7 years ago

Last modified 7 years 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 by dhoese, 7 years ago

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 7 years ago by dhoese (previous) (diff)

comment:2 by Even Rouault, 7 years ago

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 by Even Rouault, 7 years ago

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 by Even Rouault, 7 years ago

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

comment:5 by dhoese, 7 years ago

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.