如何通过Python为Python脚本创建Mac应用程序包

4 投票
3 回答
9289 浏览
提问于 2025-04-17 02:13

我想创建一个简单的Mac应用程序包,这个包里会调用一个简单的Python脚本。我想用Python来实现这个。

有没有简单的方法呢?

我试过用py2app,但总是出问题,比如:

from setuptools import setup
setup(app=["foo.py"], setup_requires=["py2app"])

结果是:

---------------------------------------------------------------------------
SystemExit                                Traceback (most recent call last)
/Users/az/<ipython console> in <module>()

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.pyc in setup(**attrs)
    138         ok = dist.parse_command_line()
    139     except DistutilsArgError, msg:
--> 140         raise SystemExit, gen_usage(dist.script_name) + "\nerror: %s" % msg
    141 
    142     if DEBUG:

SystemExit: usage: ipython [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: ipython --help [cmd1 cmd2 ...]
   or: ipython --help-commands
   or: ipython cmd --help

error: no commands supplied
Type %exit or %quit to exit IPython (%Exit or %Quit do so unconditionally).

我还试过:

import py2app.build_app
py2app.build_app.py2app("foo.py")

但也不行(TypeError: dist must be a Distribution instance),我其实不太清楚怎么使用py2app.build_app.py2app,而且也没找到很多相关的例子或文档。

也许setuptools/py2app对我来说有点复杂。我只是想创建一个简单的空应用程序包,把一个Python脚本复制进去,然后配置它的Info.plist,让它能调用这个Python脚本。

3 个回答

0

cxFreeze 是一个很好的解决方案,因为它简单又省时间。

首先,使用 Python 创建你的程序或应用,然后为你的应用制作一个setup 文件。

接着,使用构建命令 python setup.py build 来构建应用。根据你的需求,可能需要做一些调整。如果你想制作 Mac 的应用包或 Mac 应用,可以参考这里

4

可以看看 PyInstaller

你只需要告诉它你的Python脚本的路径,它就会分析你代码中用到的所有包和库,提取出需要的二进制文件,并把它们放到一个压缩包里。

我用它处理过一个比较复杂的Python程序,效果很好。

10

这正是我想要的,运行得很好:

#!/usr/bin/python

import sys
assert len(sys.argv) > 1

apppath = sys.argv[1]

import os, os.path
assert os.path.splitext(apppath)[1] == ".app"

os.makedirs(apppath + "/Contents/MacOS")

version = "1.0.0"
bundleName = "Test"
bundleIdentifier = "org.test.test"

f = open(apppath + "/Contents/Info.plist", "w")
f.write("""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>English</string>
    <key>CFBundleExecutable</key>
    <string>main.py</string>
    <key>CFBundleGetInfoString</key>
    <string>%s</string>
    <key>CFBundleIconFile</key>
    <string>app.icns</string>
    <key>CFBundleIdentifier</key>
    <string>%s</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>%s</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>%s</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>%s</string>
    <key>NSAppleScriptEnabled</key>
    <string>YES</string>
    <key>NSMainNibFile</key>
    <string>MainMenu</string>
    <key>NSPrincipalClass</key>
    <string>NSApplication</string>
</dict>
</plist>
""" % (bundleName + " " + version, bundleIdentifier, bundleName, bundleName + " " + version, version))
f.close()

f = open(apppath + "/Contents/PkgInfo", "w")
f.write("APPL????")
f.close()

f = open(apppath + "/Contents/MacOS/main.py", "w")
f.write("""#!/usr/bin/python
print "Hi there"
""")
f.close()

import stat
oldmode = os.stat(apppath + "/Contents/MacOS/main.py").st_mode
os.chmod(apppath + "/Contents/MacOS/main.py", oldmode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)

撰写回答