python-3.x - 使用 pyinstaller 将 python 脚本构建为单个 exe

标签 python-3.x pandas pyinstaller

我遇到以下错误:脚本名称 = 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

我在链接的帮助下使用 Pandas 风格:Change color of complete row in data frame in pandas

我看到样式正在使用 jinja2 导致上述错误。有没有办法 Hook 这个错误或任何其他工具来将 python 脚本转换为单个可执行文件。

最佳答案

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

问题不在于 Hook (这是我第一次尝试解决方案),而是 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")

将此行更改为:
template = env.from_string("html.tpl")

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

希望能帮助到你。

关于python-3.x - 使用 pyinstaller 将 python 脚本构建为单个 exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49753373/

相关文章:

python - 是/否在 Python3 中使用 strtobool 提示

python - 在最近的时间戳上合并两个 Pandas 数据帧

python - 使用 panda 和 matplotlib 设置 xtick 最大值

python - pyinstaller错误: OSError: Python library not found: libpython3. 4mu.so.1.0, libpython3.4m.so.1.0, libpython3.4.so.1.0

python - 我可以让 PyInstaller 优化编译吗?

python - 在 xgboost 上使用 shap 时出现 UnicodeDecodeError

python - 如何从行中获取数据到元组中

python - 美丽汤 : how to find all elements 2nd parent of which has exact attribute?

python - 如何从 Dataframe 指定日期时间中的年、月、日?

python - 如何编码以修复检测到的 fatal error : Failed to execute script BoxDetection after use auto-py-to-exe compile python code to exe file?