PyInstaller 中没有名为“pandas._libs.tslibs.timedeltas”的模块
- 2025-04-10 09:46:00
- admin 原创
- 17
问题描述:
我正在尝试使用适用于 Windows 的 PyInstaller(开发版)将 Python 脚本包装成 exe。
该脚本使用了 Pandas,我在运行 exe 时遇到了错误。
Traceback (most recent call last): File "site-packagespandas__init__.py", line 26, in <module> File "C:UsersEddieAnaconda3libsite-packagesPyInstallerloaderpyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__) File "site-packagespandas_libs__init__.py", line 4, in <module> File "C:UsersEddieAnaconda3libsite-packagesPyInstallerloaderpyimod03_importers.py", line 714, in load_module
module = loader.load_module(fullname) File "pandas/_libs/tslib.pyx", line 1, in init pandas._libs.tslib ModuleNotFoundError: No module named 'pandas._libs.tslibs.timedeltas'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "G5k Version file Extract (with tkinter).py", line 15, in <module> File "C:UsersEddieAnaconda3libsite-packagesPyInstallerloaderpyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__) File "site-packagespandas__init__.py", line 35, in <module> ImportError: C extension: No module named 'pandas._libs.tslibs.timedeltas' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.
我曾尝试对没有熊猫的程序执行此操作,一切都很好。
这与另一个已针对 Python 2 解决的问题非常相似,但我使用的是 Python 3,并且由于 .spec 文件格式的改变,该解决方案不能以相同的方式应用。
Python 3.6
PyInstaller - 版本 3.3
Pandas - 版本 0.20.3
解决方案 1:
PyInstaller 3.3、Pandas 0.21.0、Python 3.6.1。
由于尚未发布/提交的 PyInstaller 修复程序,我能够解决这个问题,请参阅这个和这个。并且保留将其打包成一个可执行文件的能力。
基本上:
找到 PyInstaller 文件夹..\hooks,例如
C:Program FilesPythonLibsite-packagesPyInstallerhooks
。创建文件 hook-pandas.py 及其内容(或根据您的错误创建任何类似的内容):
hiddenimports = ['pandas._libs.tslibs.timedeltas']
保存它+我删除了 .spec 文件、build 和 dist 文件夹以确保万无一失。
跑步
pyinstaller -F my_app.py
。
只要您不升级或重新安装 PyInstaller,此修复就应该有效。因此您无需编辑 .spec 文件。
也许他们会尽快为我们提供修复!:)
解决方案 2:
我不确定它是否能帮到你,但是按照你提到的帖子中的解决方案,在 Windows 7 上使用 python 3.6 pyinstaller 3.3 和 pandas 0.21.0 对我来说是可行的。
因此在分析之后立即将其添加到 spec 文件中:
def get_pandas_path():
import pandas
pandas_path = pandas.__path__[0]
return pandas_path
dict_tree = Tree(get_pandas_path(), prefix='pandas', excludes=["*.pyc"])
a.datas += dict_tree
a.binaries = filter(lambda x: 'pandas' not in x[0], a.binaries)
另外,我的规范文件格式与您提到的帖子中的格式相同。
解决方案 3:
我设法通过使用“--hidden-import”标志解决了这个问题。希望这能对遇到此主题的其他人有所帮助。
pyinstaller --onefile --hidden-import pandas._libs.tslibs.timedeltas myScript.py
解决方案 4:
如果你正在使用 Anaconda,那么当你尝试卸载某个包时,它很可能破坏了 pandas 依赖关系,导致无法获取所需的脚本。如果你直接运行,conda install pandas
可能会出现另一个错误:
module 'pandas' has no attribute 'compat'
。
因此,请尝试卸载并重新安装 pandas conda uninstall pandas
,然后再次使用此方法安装conda install pandas
即可解决问题。另一方面,如果您没有使用 Anaconda,请尝试在指向 Python 脚本文件夹的命令提示符上执行相同操作pip uninstall pandas & pip install pandas
。
大多数情况下,这应该可以解决问题。为了涵盖所有可能性,安装 pandas 后不要忘记从 Anaconda 启动 Spyder。
扫码咨询,免费领取项目管理大礼包!