importError c extension: No module named np_datetine not buit

2018-10-1 杜世伟 Python

我的python代码中有

import pandas

使用pyinstaller进行打包exe的时候出现以下问题,现在就来说一下。
打包的时候没有报错,

但是执行时候首先报了pandas的错。提示没有找到pandas._lilbs.tslibs.np_datetime。大概的错误如下:

第一个错误是:

Fi1e sitepackagesp\pandas\init .py 1ine 35 in Kmodule?

importError c extension: No module named np_datetine not buit. Jf you yant to import pandas from. the source drectory

g you may need to run python setup. py buildext inplace force to bui1d the c extensions first:

iFai1ed to execute script smg



通过网上查找的方法,修改下pyinstaller的用法,生成过程中添加--hiddenimport=pandas._libs.tslibs.np_datetime,代码如下

pyinstaller -F -w smg.py  --hiddenimport=pandas._libs.tslibs.np_datetime

继续打包,过程中没有报错,继续执行exe文件的时候报错如下

第二个错误:
Fi1e sitepackagesp\pandas\init .py 1ine 35 in Kmodule?

Fi1e sitepackagesp\pandas\init .py 1ine 35 in Kmodule?

importError c extension: No module named timedeltas not buit. Jf you yant to import pandas from. the source drectory

g you may need to run python setup. py buildext inplace force to bui1d the c extensions first:

iFai1ed to execute script smg


郁闷了又出现了新的错误信息。。。。。
卸载pyinstaller重新安装还是不行
继续网上搜索:
原因是pandas是c源码,而pyInstaller找不到pandas的代码,就算你是-p /lib/site-packages还是不行。
解决方法:
在./Lib/site-packages/PyInstaller/hooks下,新建一个文件名为hook-pandas.py。写入以下内容并保存。

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

按照上面的步骤操作,然后继续打包,最终成功运行了exe文件

参照:
https://blog.csdn.net/cyx441984694/article/details/80883554
https://stackoverflow.com/questions/47318119/no-module-named-pandas-libs-tslibs-timedeltas-in-pyinstaller

标签: pandas np_datetime timedeltas

Powered by emlog 沪ICP备2023034538号-1