6

I am running a virtual environment with Python 2.7 for my program. There seems to be a problem after creating the executable file on windows. I ran venv/Scripts/pyinstaller.exe -F main.py everything seems fine. But when i click on the created executable main.exe. There is an error.

Tried and tested

  1. I have re-installed of pandas and pyinstaller
  2. Implemented the hook-pandas.py to the hooks folder in the environment. hook-pandas
  3. Ensured the environment is activated.
  4. Checked that the program is running fine before building executable.
  5. Re-created the environment.

Yet after all that, I am prompted with this issue [see Importerror] when I run the executable file.

enter image description here

It is an extreme pain to debug this because the command prompt displaying the error will not pause but close almost immediately.

Similar issues

Looking for Suggestions I am hoping for suggestions to troubleshoot Pyinstaller. Any resources to read up on would be nice. Usually, I have no trouble with python as Pycharm has several handy debugging tools that will help me identify the problem

1 Answer 1

16

I ran into the same problem and found this thread, but I managed to solve it borrowing from the reference you posted (about pandas._libs.tslibs.timedeltas), so thank you for that!

In that article, the module that resulted in the ImportError was, in fact pandas._libs.tslibs.timedeltas, if you look at the poster's logs. But the error you and I ran into refers to np_datetime instead. So, from the traceback logs, I finally figured out that the code we have to write in hook-pandas.py should be the following:

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

Maybe that alone will solve your problem, HOWEVER, in my case, once I solved the np_datetime issue, other very similar ImportError problems arose (also related to hiddenimports regarding pandas), so, in case you run into the same issues, just define hiddenimports as follows:

hiddenimports = ['pandas._libs.tslibs.np_datetime','pandas._libs.tslibs.nattype','pandas._libs.skiplist']

TL;DR:

You can first try to write

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

into hook-pandas.py. However, if for some reason you run into the exact same issues I did afterwards, try

hiddenimports = ['pandas._libs.tslibs.np_datetime','pandas._libs.tslibs.nattype','pandas._libs.skiplist']

If you wish to dive deeper (or run into a different pandas ImportError than the ones I did), this is the code in pandas's __init__.py referenced in your traceback log (lines 23 to 35):

from pandas.compat.numpy import *

try:
    from pandas._libs import (hashtable as _hashtable,
                             lib as _lib,
                             tslib as _tslib)
except ImportError as e:  # pragma: no cover
    # hack but overkill to use re
    module = str(e).replace('cannot import name ', '')
    raise ImportError("C extension: {0} 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.".format(module))

From that I went into the

C:\Python27\Lib\site-packages\pandas_libs

and

C:\Python27\Lib\site-packages\pandas_libs\tslibs

folders and found the exact names of the modules that resulted the errors.

I hope that solves your problem as it did mine.

Cheers!

3
  • The first solution gave me the same problem too. The second solution works for me everything works great now.Thanks for helping me understand how to debug this too. Truly appreciate it! :D
    – Muzamir
    Jun 11, 2018 at 4:38
  • Thanks! you saved me a headache :) Jul 11, 2018 at 14:38
  • Thanks, this saved me a lot of time!
    – maggie
    Apr 30, 2019 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.