微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pywebview - 如何将 pywebview 应用程序冻结为较小的可执行文件?

如何解决Pywebview - 如何将 pywebview 应用程序冻结为较小的可执行文件?

我的目标:

在 Linux(特别是 Arch Linux)上捆绑一个简单的 pywebview 应用程序作为独立的可执行文件一个文件)。二进制可执行文件的大小必须不超过 30MB代码如下。

问题:

尝试编译以下应用程序(名为 foobar.py)时,以及在 pyinstaller 中使用以下命令时:

# install my application's dependencies
pip install pywebview

# install pyinstaller
pip install pyinstaller

# install pywebview dependencies
pip install PyGObject
pyinstaller --exclude-module PyQt5 --exclude-module sympy --exclude-module numpy foobar.py

生成的可执行文件大小为 3.1 GB。

当前无效的解决方

使用 python 虚拟环境并运行与上面相同的命令。可执行文件大小没有明显减少。

当前源代码

foobar.py

import webview

html = """

<!DOCTYPE html>
<html>
  <head>
    <title>Example Domain</title>

    <Meta charset="utf-8" />
    <Meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <Meta name="viewport" content="width=device-width,initial-scale=1" />
    <style type="text/css">
      body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system,system-ui,BlinkMacSystemFont,"Segoe UI","Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif;
      }
      div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        Box-shadow: 2px 3px 7px 2px rgba(0,0.02);
      }
      a:link,a:visited {
        color: #38488f;
        text-decoration: none;
      }
      @media (max-width: 700px) {
        div {
          margin: 0 auto;
          width: auto;
        }
      }
    </style>
  </head>

  <body>
    <div>
      <h1>Hello World</h1>
        <p>Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
        <p>
        <a href="https://example.org">More information...</a>
      </p>
    </div>
  </body>
</html>
"""

webview.create_window('Hello world',html=html)
webview.start()

解决方法

这是我的解决方案:

  • 在 virtualbox 中使用 Ubuntu Linux 创建虚拟机
  • 删除 /usr/local/themes 中的所有主题和 /usr/local/icons 中除 default/ 文件夹之外的所有图标
  • 然后,再次尝试使用 pyinstaller
  • 您的二进制文件大小约为 80 MB(小 37 倍)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。