4

我想创建一个简单的 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 脚本复制到其中并以调用 Python 脚本的方式配置其 Info.plist。

4

3 回答 3

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)
于 2011-09-13T16:37:40.127 回答
4

查看PyInstaller

你给它你的 python 脚本的路径,它会分析你所有的包导入,提取任何需要的二进制文件并将它们放入存档中。

我已经将它用于一个相当复杂的 python 程序,它对我有用。

于 2011-09-13T15:55:05.160 回答
0

cxFreeze是可用的最佳解决方案,因为它简单且省时。

首先,使用 python 创建您的程序或应用程序,然后为您的应用程序制作安装文件。

然后使用 build 命令构建应用程序python setup.py build,根据您的要求,您需要进行一些更改,以制作 mac bundle 或 mac app参考此

于 2018-11-28T11:45:06.070 回答