You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyInstaller打包Python脚本为Windows可执行文件时遇C扩展导入错误

Fixing PyInstaller Import Error with Pandas C Extension (timedeltas Module)

Hey there, let's work through this frustrating PyInstaller + pandas packaging issue you're dealing with. The error about the timedeltas C extension not being built usually happens because PyInstaller fails to detect and include pandas' compiled C modules during the packaging process, even if your pandas installation works fine normally. Here are targeted fixes to try, building on the troubleshooting you've already done:

1. Explicitly tell PyInstaller to include the missing module

The quickest first step is to use the --hidden-import flag to force PyInstaller to package the missing timedeltas C extension. Run your packaging command like this:

pyinstaller --hidden-import pandas._libs.tslibs.timedeltas your_script.py

If you get similar errors for other pandas C modules later, just add more --hidden-import entries (e.g., pandas._libs.tslibs.np_datetime) based on the error messages.

2. Upgrade PyInstaller and verify environment consistency

Older PyInstaller versions often struggle with newer pandas releases. Even if you updated pandas, make sure you're on the latest PyInstaller:

pip install --upgrade pyinstaller

Also double-check that you're running everything in the same Python environment—mixing global and virtual environments can cause path confusion that leads to missing modules.

3. Point PyInstaller directly to your pandas installation

If PyInstaller is picking up the wrong pandas instance or can't locate the right files, use the --paths flag to specify the exact site-packages directory where pandas is installed:

pyinstaller --paths "C:\YourPythonVersion\Lib\site-packages\pandas" your_script.py

Replace C:\YourPythonVersion with your actual Python installation path (e.g., C:\Python310 for Python 3.10).

4. Tweak PyInstaller's pandas hook file

PyInstaller uses "hook" files to handle complex packages like pandas, but sometimes these hooks are incomplete. Here's how to fix it:

  • Locate the hook-pandas.py file in your PyInstaller installation (usually in Lib\site-packages\PyInstaller\hooks).
  • Open it and add 'pandas._libs.tslibs.timedeltas' to the hiddenimports list:
    hiddenimports = [
        # Keep existing entries, add this line
        'pandas._libs.tslibs.timedeltas',
    ]
    
  • Save the file and re-run your packaging command.

If you can't find the hook file, create a new hook-pandas.py in your script's directory with the code above, then run:

pyinstaller --additional-hooks-dir . your_script.py

5. Re-compile pandas C extensions in your active environment

You mentioned running python setup.py build_ext --inplace, but let's ensure you're doing this in the right context:

  1. Activate your Python environment (if using a virtual env).
  2. Clone the pandas repo locally (or use your existing source code):
    git clone https://github.com/pandas-dev/pandas.git
    cd pandas
    
  3. Force a full re-compile of the C extensions:
    python setup.py build_ext --inplace --force
    
  4. Either move your script into this pandas source directory or update your Python path to prioritize this local build, then run PyInstaller again.

Start with the first three fixes—they're the simplest and most likely to resolve your issue quickly. If those don't work, move on to the hook tweak or re-compilation step.

内容的提问来源于stack exchange,提问作者luckypanda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:48:14