Search code examples
pythonpandaspyinstaller

Issues with freezing Python Pandas


I just upgraded from Python 2.7 to 3.6. I have a rather large script with a GUI which I had frozen to an .exe file with pyinstaller.

I have now made a few changes to the .py script and it works with the new Python version before I freeze it.

However, when I freeze I get a "Failed to execute script" message. I tried to look into the error code and I traced the issue to the Pandas module. Here is the error message:

C:\Users\MRCH\dist>My_File.exe
Traceback (most recent call last):
  File "site-packages\pandas\__init__.py", line 26, in <module>
  File "c:\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pandas\_libs\__init__.py", line 4, in <module>
  File "c:\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_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 "My_File.py", line 6, in <module>
  File "c:\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pandas\__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.**
[2032] Failed to execute script My_File

The error message suggest to import pandas by running 'python setup.py build_ext --inplace --force' first. Where would I run this? It can't find the setup.py file if I run it from cmd?


Solution

  • @apogalacticon Thanks!

    Adding the following line to the .spec file solved the problem:

    hiddenimports = ['pandas._libs.tslibs.timedeltas']