首页 > 解决方案 > pyinstaller ImportError: C extension: No module named np_datetime not built

问题描述

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

标签: pyinstallertimedelta

解决方案


我遇到了同样的问题并找到了这个线程,但我设法从你发布的参考资料中解决了它(关于pandas._libs.tslibs.timedeltas),所以谢谢你!

ImportError在那篇文章中,实际上pandas._libs.tslibs.timedeltas,如果您查看发布者的日志,则导致的模块是。但是你和我遇到的错误是指代的np_datetime。所以,从回溯日志中,我终于知道我们要写的代码hook-pandas.py应该是这样的:

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

也许仅此一项就可以解决您的问题,但是,就我而言,一旦我解决了该np_datetime问题,就会出现其他非常相似的ImportError问题(也与有关 pandas 的 hiddenimports 相关),因此,如果您遇到相同的问题,只需定义hiddenimports如下:

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

TL;博士:

你可以先尝试写

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

进入hook-pandas.py. 但是,如果由于某种原因您遇到了我后来遇到的完全相同的问题,请尝试

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

如果您想深入了解(或遇到与ImportError我不同的 pandas),这是__init__.py您的回溯日志中引用的 pandas 中的代码(第 23 到 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))

从此我进入了

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

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

文件夹并找到导致错误的模块的确切名称。

我希望这能解决你的问题,就像我的问题一样。

干杯!


推荐阅读