关键字: subprocess, pandas, 多个py打包成一个py, PyInstaller
我的代码中有
import pandas
import subprocess
在打包过程中遇到了几个问题,现在就来说一下。
打包的过程中没有问题,但是执行时候首先报了pandas的错。提示没有找到pandas._lilbs.tslibs.np_datetime。大概的错误如下:
ModuleNotFoundError: No module named 'pandas._libs.tslibs.np_datetime'
'pandas._libs.tslibs.np_datetime' 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
原因是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']
上面是基本包括了pandas的了。所以基本import pandas的都可以用这个来解决。
以下是其他的一些答案(这些答案只适用于部分的pandas,或者系统。我这些都试过都不行...)
1. pyinstaller的用法改一下
pyinstaller --clean --win-private-assemblies -F -w test.py --hiddenimport=pandas._libs.tslibs.np_datetime
2. 在specs中修改,然后重新用specs生成exe
hiddenimport=[
"pandas","pandas._libs.tslibs.np_datetime"]
3. 一样修改specs。
source:https://stackoverflow.com/questions/33001327/importerror-with-pyinstaller-and-pandas?rq=1
a = Analysis(...)
# Add the following
def get_pandas_path():
import pandas
pandas_path = pandas.__path__[0]
return pandas_path
4. 把lib.so复制到dist的目录中,并重命名为pandas.lib.so
fixed this issue on my end. Pyinstaller wasn't including `$PYTHONDIR/site-
packages/pandas/lib.so`` just copied this into the dist directory and named it pandas.lib.so and
it appears to be working. Pyinstaller is awesome keep up the good work!
source:https://github.com/pyinstaller/pyinstaller/issues/1580
>>>>>>>>>>>>>>>>>>>>我是分割线>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
解决了pandas的问题后,又遇到了subprocess的问题。执行exe后报以下的错误:
Traceback (most recent call last):
File "tem-csv.py", line 363, in
main()
File "tem-csv.py", line 34, in main
child = subprocess.Popen("auto-crt.bat", cwd=getpath, stdout=subprocess.PIPE
, startupinfo=si, env=env)
File "subprocess.py", line 709, in __init__
File "subprocess.py", line 997, in _execute_child
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
[2444] Failed to execute script tem-csv
代码关于subprocess的引用如下:
child=subprocess.Popen("auto-crt.bat",cwd=getpath,stdout=subprocess.PIPE)
stdout,stderr=child.communicate()
解决方法:
把subprocess的参数都补全
child = subprocess.Popen("auto-crt.bat", cwd=getpath, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout,stderr=child.communicate()
谢谢心善的你看到最后。
希望能帮到你。