使用 Nuitka 编译 Python 程序

Nuitka 官网:nuitka.net

  1. 安装 Nuitka 包

    pip install nuitka
    
  2. 写一个示例 Python 程序

    vim main.py
    
    def talk(message):
        return "Talk " + message
    
    def main():
        print(talk("Hello World"))
    
    if __name__ == "__main__":
        main()
    
  3. 使用 Nuitka 编译 Python 程序

    python -m nuitka main.py
    
  4. 第一次编译时 Nuitka 会询问你是否允许下载 Ccache 加速编译,回答 y

    Nuitka will make use of ccache to speed up repeated compilation.
    
    Is it OK to download and put it in '/Users/username/Library/Caches/Nuitka/downloads/ccache/v4.2.1'.
    
    Fully automatic, cached. Proceed and download? [Yes]/No : y
    
  5. 编译完成,编译产物为 main.bin。运行编译产物:

    $ ./main.bin
    Talk Hello World
    
posted @ 2025-01-13 13:07  Undefined443  阅读(79)  评论(0)    收藏  举报