Pyinstaller 无法导入 geopandas

Pyinstaller cannot import geopandas

我正在尝试从使用 pyinstaller 创建的可执行文件中使用 geopandas 库。但是它抛出了一个关于 dll 文件丢失的错误。当作为 python 文件执行时,我可以 运行 脚本,但不能作为可执行文件执行。

这是我正在使用的脚本(名为 sample.py):

import geopandas as gpd
print("Hello world")

这是我遇到的错误:

(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!        

我执行了 pyinstaller sample.py 来创建可执行文件。

这是 pyinstaller 日志的 pastebin link:https://pastebin.com/pjyYbVB8

您可以将 --collect-binaries rtree 添加到 pyinstaller 命令,如果
您正在使用 pyinstaller 4.3 或更高版本。

spatial*.dll 位于 rtree 包内:

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

PyInstaller 将收集 dll 并将它们放入 dist/ 文件夹。