Python到可执行文件的大小优化

huangapple go评论63阅读模式
英文:

python to executable size optimization

问题

我要优化这个。

英文:

I'm going to build python to executable but the size is big(about 6 MB).
I'd like to reduce size less than 4 MB.

Let me know how to optimize that.
Thanks.

from requests import get, post
from sseclient import SSEClient
import time
import subprocess

#set server url
server_uri = 'http://127.0.0.1/server.php'
try:
    my_id = get('https://api.ipify.org').text

    try:
        server = get(server_uri)
        print(server.headers)

        messages = SSEClient(server_uri)
        subprocess.call('ipconfig', shell=True) 

    except Exception as e:
        print("you can't connect to server\n")

except Exception as e:
    print("Please check your internet connection\n")

Let me know how to optimize that.

答案1

得分: 2

如果您正在使用pyinstaller创建可执行文件,请检查是否已创建虚拟环境并仅安装所需的模块。在某些情况下,如果未为项目创建专用虚拟环境,可能会打包所有模块。

英文:

If you are using pyinstaller to create executable, check if you have virtual env. created and only required modules are installed . in some cases all modules are packaged if not dedicated virtual env is created for project .

答案2

得分: 1

  1. 选择一个更小的Python发行版:有时,像Anaconda这样的发行版包含大量不必要的包。如果是这种情况,尝试使用更小的发行版,如Miniconda,并只安装你的脚本实际使用的内容。

  2. 使用PyInstaller或cx_Freeze:这些工具允许你从Python脚本创建一个独立的可执行文件,并包含所有依赖项。它们甚至可以压缩文件以减小大小。

  3. 尝试不同的编程语言:如果你真的渴望使可执行文件非常小,可以考虑切换到像Go或Rust这样产生较小可执行文件的不同编程语言。

英文:

I think to shrink the size of your Python executable

  1. Pick a smaller Python distribution: Sometimes, distributions like Anaconda have tons of packages you don't need. If that's the case, try using a smaller distribution like Miniconda and only install the stuff your script actually uses.

  2. Use PyInstaller or cx_Freeze: These tools let you make a standalone executable from a Python script that has all the dependencies included. They can even compress the file to make it smaller.

  3. Try a different programming language: If you're really desperate to make your executable teeny-tiny, you could think about switching to a different language like Go or Rust that produces smaller executables.

huangapple
  • 本文由 发表于 2023年3月7日 23:23:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定