使用PyInstaller创建包含Pandas的.exe文件时出现错误

3

我正在使用Python 3.7.3和PyInstaller 4.0,并且在Windows 10上操作。我的脚本如下:

import pandas as pd
print('hello')

但是当我尝试运行.exe文件时,出现了一个错误。

到目前为止,我已经尝试了以下方法:

pyinstaller --hidden-import=pandas --onefile myscript.py

但是它不起作用。我还更新到了这里的当前开发版本:https://pyinstaller.readthedocs.io/en/stable/installation.html 此外,我编辑了.spec文件并写入:
# -*- mode: python -*-block_cipher = Nonedef get_pandas_path():
    import pandas
    pandas_path = pandas.__path__[0]
    return pandas_patha = Analysis(['FIFA.py'],
             pathex=['C:\\Users\\NBenton\\PycharmProjects\\RES3D_BETA'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)

dict_tree = Tree(get_pandas_path(), prefix='pandas', excludes=["*.pyc"])
a.datas += dict_tree
a.binaries = filter(lambda x: 'pandas' not in x[0], a.binaries)pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='FIFA',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

然后运行

pyinstaller myscript.spec --onefile

我知道这是一个常见的问题,但其他问题中的答案对我不起作用。

有什么帮助吗?谢谢

经过很多行,错误是:

File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\hooks\hook-numpy.core.py", line 29, in <module>
    pkg_base, pkg_dir = get_package_paths('numpy.core')
  File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 528, in get_package_paths
    file_attr = get_module_file_attribute(package)
  File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 330, in get_module_file_attribute
    raise ImportError
ImportError


你的 hook-pandas.py 文件长什么样? - It_is_Chris
它将位于类似于\AppData\Local\Programs\Python\Python36\Lib\site-packages\PyInstaller\hooks的位置。 - It_is_Chris
从PyInstaller.utils.hooks导入collect_submodules#Pandas将使用动态导入加载的Python扩展程序保存在此处。 hiddenimports = collect_submodules('pandas._libs') - LizUlloa
运行pyinstaller时,您看到了哪些警告?例如“警告:未找到隐藏的导入pandas._libs.tslibs.timedeltas”。 - It_is_Chris
2个回答

3

hook-pandas.py

hook-pandas.py文件中添加datas = collect_data_files('pandas')对我有用。

from PyInstaller.utils.hooks import collect_submodules
from PyInstaller.utils.hooks import collect_data_files

# Pandas keeps Python extensions loaded with dynamic imports here.
hiddenimports = collect_submodules('pandas._libs')
datas = collect_data_files('pandas')

Anaconda提示符

我还添加了隐藏导入pkg_resources.py2_warn,因为那是我收到的一个错误。

(base) C:\Users\...\test_folder>pyinstaller test.py -F --hidden-import pkg_resources.py2_warn
102 INFO: PyInstaller: 3.6
102 INFO: Python: 3.7.6 (conda)
106 INFO: Platform: Windows-10-10.0.18362-SP0
116 INFO: wrote C:\Users...\test_folder\test.spec
120 INFO: UPX is not available.
125 INFO: Extending PYTHONPATH with paths

注意: 在pyinstaller test.py -F --hidden-import pkg_resources.py2_warn命令中,-F--onefile相同。

test.py

import pandas as pd
print('hello world')
input()

2
嗯,又是pandas的问题。你能否看一下这个问题并检查一下我们的hooks是否需要更新?如果需要,请提交一个PR。(我是PyInstaller开发者) - Legorooj
@Legorooj,我有时间会看一下。感谢你对pyinstaller的工作;这是一个很棒的软件包。 - It_is_Chris

1

据报道,pyinstaller存在此问题这里

  1. 根据该线程建议设置您的隐藏导入项:
hiddenimports = ['pandas._libs.tslibs.timedeltas',
'pandas._libs.tslibs.nattype',
'pandas._libs.tslibs.np_datetime',
'pandas._libs.skiplist']
  1. 如果那不起作用,请尝试使用pip安装pandas

    pip install pandas 而不是通过conda安装

我很惊讶看到Pyinstaller的版本4.0,因为据我所知它还没有发布。


我不认为这样会奏效,因为OP指出所有来自pandas._libs的隐藏导入都被收集在hook-pandas.py文件中:hiddenimports = collect_submodules('pandas._libs') - It_is_Chris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接