使用 pyinstaller 将 python 脚本构建为单个 exe

avi*_*hse 7 pyinstaller python-3.x pandas

我收到以下错误:脚本名称 = prepareIncidentCountMail.py

Traceback (most recent call last):
  File "Alexa\prepareIncidentCountMail.py", line 52, in <module>
  File "site-packages\pandas\core\frame.py", line 683, in style
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "c:\users\avikumar\documents\learn\alexa\venv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pandas\io\formats\style.py", line 50, in <module>
  File "site-packages\pandas\io\formats\style.py", line 118, in Styler
  File "site-packages\jinja2\environment.py", line 830, in get_template
  File "site-packages\jinja2\environment.py", line 804, in _load_template
  File "site-packages\jinja2\loaders.py", line 113, in load
  File "site-packages\jinja2\loaders.py", line 234, in get_source
  File "site-packages\pkg_resources\__init__.py", line 1459, in has_resource
  File "site-packages\pkg_resources\__init__.py", line 1509, in _has
NotImplementedError: Can't perform this operation for unregistered loader type
[10536] Failed to execute script prepareIncidentCountMail
Run Code Online (Sandbox Code Playgroud)

我在链接的帮助下使用熊猫样式:更改熊猫数据框中完整行的颜色

我看到 style 正在使用 jinja2 导致上述错误。有什么方法可以挂钩此错误或任何其他工具将 python 脚本转换为单个可执行文件。

小智 8

我昨天刚刚使用 giumas 在这里所做的调整版本自己解决了这个问题:https : //github.com/pyinstaller/pyinstaller/issues/1898

问题不在于挂钩(这是我第一次尝试解决方案),而是 Pandas 样式模块导入 jinja2 的事实,它使用“get_template”方法,而后者又使用 pkg_resources 模块。最后一个是问题,由于某种原因 pyinstaller 不能很好地与 pkg_resources 模块配合使用。

解决方案:找到pandas的安装位置并转到类似

C:\Users\UserName\AppData\Local\Programs\Python\Python36\Lib\site-packages\pandas\io\formats

在格式文件夹中找到 style.py 文件并在您喜欢的文本编辑器中打开它。在 style.py 中向下滚动到第 118 行,您将在其中看到:

template = env.get_template("html.tpl")
Run Code Online (Sandbox Code Playgroud)

将此行更改为:

template = env.from_string("html.tpl")
Run Code Online (Sandbox Code Playgroud)

保存文件并重新运行pyinstaller。当您尝试运行可执行文件时,它应该按预期执行,没有任何错误消息。

希望能帮助到你。