Search code examples
pythonpyinstallergeopandas

Pyinstaller cannot import geopandas


I am trying to use geopandas library from an executable created using pyinstaller. But it is throwing an error about a dll file missing. I can run the script when executing as a python file, but not as an executable file.

This is the script I am using (named sample.py):

import geopandas as gpd
print("Hello world")

This is the error I am encountering:

(venv) C:\Users\Windows 10\Desktop\py2exe\dist\sample>sample.exe
Traceback (most recent call last):
  File "sample.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load        
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module 
  File "geopandas\__init__.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load        
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module 
  File "geopandas\_config.py", line 126, in <module>
  File "geopandas\_config.py", line 112, in _default_use_pygeos
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load        
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module 
  File "geopandas\_compat.py", line 202, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load        
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module 
  File "rtree\__init__.py", line 9, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load        
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module 
  File "rtree\index.py", line 6, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load        
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module 
  File "rtree\core.py", line 75, in <module>
  File "rtree\finder.py", line 67, in load
OSError: could not find or load spatialindex_c.dll
[8276] Failed to execute script 'sample' due to unhandled exception!        

I executed pyinstaller sample.py to create the executable.

This is the pastebin link to the pyinstaller logs: https://pastebin.com/pjyYbVB8


Solution

  • You can add --collect-binaries rtree to the pyinstaller command if
    you're using pyinstaller 4.3 or above.

    The spatial*.dlls are located inside the rtree package:

    |   core.py
    |   exceptions.py
    |   finder.py
    |   index.py
    |   __init__.py
    |
    \---lib
            spatialindex-64.dll
            spatialindex_c-64.dll
    

    PyInstaller will collect the dlls and put them inside the dist/ folder.