小工具知识网

当前位置: 首页 > 行业知识 > python打包成mac可执行文件,python打包mac可执行文件

行业知识

python打包成mac可执行文件,python打包mac可执行文件

2026-11-06 11:25:43 来源:互联网转载

大家好,小编为大家解答python打包成mac可执行文件的问题。很多人还不知道python打包mac可执行文件,现在让我们一起来看看吧!

PyInstaller:可执行文件,也适用于Windows(亲测可用)

       

注意:之前网上有说Python新版本不支持使用PyInstaller,本人亲测是支持的,请大家放心。

        PyInstaller是同时支持Windows和macOS的软件,它有一点比较好:在macOS上打包的便是UNIX可执行文件,在Windows上打包的便是exe文件快码论文。

        同样,我们先安装:

#Python 2:

pip install pyinstaller

 

#Python 3:

pip3 install pyinstaller

        然后cd到你脚本的目录下,打包:

#将xxx.py改为你的脚本名称

pyinstaller -F xxx.py

        打包完成之后,你将会看到“xxx.spec”文件、“__pycache__”文件夹、“build”文件夹以及“dist”文件夹。你的可执行文件同样在dist目录里。

        以上就是我为大家带来的“Python将脚本转换为可执行文件(macOS)”。

使用py2app打包

1. 安装py2app

py2app官网:https://pythonhosted.org/py2app/

安装命令:
pip install py2app

2. 准备一个python GUI 程序

2.1 首先安装一个gui框架 wxpython

参考:

http://wiki.wxpython.org/

http://wiki.wxpython.org/Getting%20Started

2.2 写代码

# !/usr/bin/env python

import wx

app = wx.App(False) # Create a new app, don't redirect stdout/stderr to a window.

frame = wx.Frame(None, wx.ID_ANY, "Hello World") # A Frame is a top-level window.

frame.Show(True) # Show the frame.

app.MainLoop()

3. 打包

3.1 cd到要打包的当前目录下

cd /Users/xiaoru/Desktop/Learning/PyToApp

3.2 生成setup文件:
py2applet --make-setup hello.py

3.3 输入打包命令:
python setup.py py2app -A

可以看到在dist目录下生成了app文件, 双击可以运行了。

注:我使用的是python3 所以全程用到的命令都是把python改成python3

上一篇:揭秘节奏大师游戏背后的神秘源码世界!

下一篇:宝塔面板手机使用指南,轻松上手,无忧操作!