如何使用 Python 创建 Mac OS X 应用程序?

发布于 2024-12-02 06:10:48 字数 1345 浏览 4 评论 0原文

我想创建一个可以在 Windows 和 Mac 上运行的 GUI 应用程序。为此我选择了Python。

问题出在 Mac OS X 上。

有 2 个工具可以为 Mac 生成“.app”:py2app 和 pyinstaller。

  1. py2app 相当不错,但它在包中添加了源代码。我 不想与最终用户共享代码。
  2. Pyinstaller生成UNIX可执行文件,那么如何在Mac上运行它呢?我 使用此可执行文件创建了一个捆绑包,但生成的“.app”是 不工作。

问题是:

  1. 如何配置 py2app 将源代码包含在 可执行文件,所以最终用户将无法访问我的程序?
  2. 如何将 UNIX 可执行文件转换为 Mac“.app”?
  3. 有没有办法用 GCC 编译 Python 代码?
  4. 在 Windows 中这很简单,我从 Python 代码创建了一个“exe”文件,然后 有用。是否可以为 Mac 创建单个文件“app”?

PS 我使用两台计算机(Windows 和 Mac)、Python 2.7、wxPython、py2exe、py2app 和 pyinstaller。

另外,我还查看了这些网站:

I want to create a GUI application which should work on Windows and Mac. For this I've chosen Python.

The problem is on Mac OS X.

There are 2 tools to generate an ".app" for Mac: py2app and pyinstaller.

  1. py2app is pretty good, but it adds the source code in the package. I
    don't want to share the code with the final users.
  2. Pyinstaller generates UNIX executable, so how to run it on Mac? I
    created a bundles with this executable, but the resulted ".app" is
    not working.

The questions are:

  1. How to configure py2app to include the source code in the
    executable, so the final users will not have access to my program?
  2. How to convert UNIX executable to Mac ".app" ?
  3. Is there a way to compile Python code with GCC ?
  4. In Windows it's easy, I created an "exe" file from Python code and
    it works. Is it possible to create a single file "app" for Mac ?

P.S. I use two computers (Windows and for Mac), Python 2.7, wxPython, py2exe, py2app and pyinstaller.

Also, I have checked out these sites:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

穿越时光隧道 2024-12-09 06:10:48

如何配置 py2app 将源代码包含在可执行文件中,
那么最终用户将无法访问我的程序?

除非你非常认真地破解 python 解释器(并包括损坏的版本),否则没有真正好的方法来向中等技能和坚定的用户隐藏源代码。我坚信 Windows 上也是如此。基本上,无论您包含真实的源代码还是字节码,都可以恢复源代码的相当干净的版本。更重要的是,在我看来,除非您包含实际的源代码(与字节码相反,您将引入对解释器版本的可能依赖)。

如何将 UNIX 可执行文件转换为 Mac“.app”?

UNIX 可执行文件是什么意思? Darwin (OS X) 二进制文件[实际上不是 UNIX]?这可以使用您已经提到的各种工具来完成,但必须小心操作以避免库依赖性。

如果您想要一个简单的包装器来将命令行二进制文件放入窗口中,那么这很容易完成,并且免费的 XCode 套件有几个可以使用的示例(取决于输出的内容)
您想要交付(如果有)。

有没有办法用 GCC 编译 Python 代码?

GCC 不编译 Python。它是一种不同的语言(尽管 gcc 系列中有支持多语言前端的工具,但不支持 Python)。有些工具尝试将 Python 转换为 C,然后您可以将其编译为真正的二进制文件,但这仅适用于避免某些类型构造的程序,并且该过程(和限制)也需要应用您的库。
Cython 是实现这一点的一个项目。它适用于某些类型
代码,大部分是数字代码,但安装和安装并不简单
漏洞利用,特别是如果你想生产一些可以在多个平台上运行的东西
不同的计算机。

在 Windows 中这很简单,我从 Python 代码创建了一个“exe”文件,它
作品。是否可以为 Mac 创建单个文件“app”?

我不得不说我对此持怀疑态度——非常怀疑。就像 OS X 的情况一样,几乎可以肯定,exe 中的源代码可以轻松访问。

一个相当简单的技巧是加密源代码,然后动态解密,但这
在我看来,这似乎带来的麻烦多于其价值。

How to configure py2app to include the source code in the executable,
so the final users will not have access to my program?

Unless you very seriously hack the python interpreter (and include the mangled version) there is no really good way to hide the source from a moderately skilled and determined user. I strongly believe this is true on Windows also. Basically, whether you include true source or bytecode, a pretty clean version of the source can be recovered. More importantly, in my opinion, unless you include the actual source code (as opposed to bytecode, you will introduce a possible dependency on the interpreter version).

How to convert UNIX executable to Mac ".app" ?

What do you mean by a UNIX executable? A Darwin (OS X) binary [which isn't actually UNIX]? That can be done using the kinds of tools you already mentioned, but it must be done carefully to avoid library dependencies.

If all you want it a simple wrapper to put a command-line binary into a window, it's pretty easy to accomplish and the free XCode suite has several examples that would serve (depending on what output
you wan to deliver, if any).

Is there a way to compile Python code with GCC ?

GCC does not compile Python. It's a different language (although there tools in the gcc family rthat support multiple language front-ends, but not Python). There are tools that attempt to translate Python into C, and then you can compile that into a true binary, but this only works for programs that avoid certain types of construct, and the process (and restrictions) need to apply your libraries as well.
One project to allow this is Cython. It works well for some types
of code, mostly numerical code, but it is not trivial to install and
exploit, very especially if you want to produce something that runs on multiple
different computers.

In Windows it's easy, I created an "exe" file from Python code and it
works. Is it possible to create a single file "app" for Mac ?

I would have to say I am skeptical -- very skeptical -- about this. Just like the OS X case, the exe almost certainly has the source code trivially accessible within it.

One fairly easy trick is to encrypt the source code and then decrypt it on the fly, but this
seems to me like more trouble than it's worth.

暗喜 2024-12-09 06:10:48

PyInstaller 将在 Mac OSX 下自动为窗口可执行文件创建捆绑包。运行 ypinstaller.py 时,请确保传递选项“--windowed”。

这个功能在pyinstaller的网站上有记录

PyInstaller will automatically create bundles under Mac OSX for windowed executables. When running ypinstaller.py, make sure to pass the option "--windowed".

This feature is documented in the website of pyinstaller

一身骄傲 2024-12-09 06:10:48

如果您没有完全致力于 wxPython(以及其他正在寻找跨平台 Python GUI 框架的人),我建议您查看 基维。它是跨平台的、GPU 加速的,并且会为您应用程序打包 。它很容易上手,具有经过深思熟虑的架构,并在界面方面为您提供了令人难以置信的灵活性。这是我发现的制作跨平台 Python GUI 应用程序的最佳方法。

If you're not completely committed to wxPython (and for anyone else looking for a cross platform Python GUI framework), I recommend you check out Kivy. It's cross platform, GPU accelerated, and it will do the app packaging for you. It's easy to jump into, has a well thought-out architecture, and gives you an incredible amount of flexibility in terms of the interface. It's the best way I've found to make a cross platform Python GUI app.

美人迟暮 2024-12-09 06:10:48

cxFreeze 是我们的选择。
我使用它将我的 python 程序打包到 Mac OS X 应用程序中。这就像一个魅力。

cxFreeze was the choice.
I use it pack my python program to a Mac OS X app. Which works like a charm.

别再吹冷风 2024-12-09 06:10:48

Automator 已经被提及作为包含的 Python 脚本的快速而简单的解决方案在单个文件中,但由于 Automator UI 有如此多的选项,实际如何操作可能并不明显,因此步骤如下:

  1. 在 Automator 中选择 File > > 新建并选择应用程序作为文档类型。
  2. 接下来,确保选择左侧的操作选项卡,然后在搜索框中键入run。在其他选项中,您将看到运行 Shell 脚本 - 双击它,右侧面板中将出现一个编辑器窗口。
  3. Shell 下拉菜单中选择 /usr/bin/python/usr/bin/python3 或您想要使用的任何其他 python。
  4. 将 Python 代码粘贴到编辑窗口中,然后选择 File > 保存

默认情况下,应用程序将保存在 $HOME/Applications 下,并显示在 Spotlight 中。

如果您希望能够设置自己的图标并拥有一些奇特的功能,例如带有菜单的任务栏图标、日志窗口等,请查看 Platypus — 一个开源用于创建 MacOS 本机捆绑包的应用程序。

Automator was already mentioned as a quick and simple solution for Pythons scripts that are contained in a single file, but since the Automator UI has so many options, it might not be obvious how to actually do it, so here are the steps:

  1. In Automator select File > New and pick Application as document type.
  2. Next, make sure Actions tab is selected on the left, and then in the search box type run. Among other options you'll see Run Shell Script — doubleclick it, and an editor window will appear in the right panel.
  3. From the Shell dropdown menu select /usr/bin/python, /usr/bin/python3 or whatever other python you want to use.
  4. Paste your Python code into the edit window and then pick File > Save.

By default, the app will be saved under $HOME/Applications and will appear in Spotlight.

If you want to be able to set your own icon and have some fancy features, like task bar icons with a menu, log windows etc, then have a look at Platypus — an open-source app for creating MacOS native bundles.

深海少女心 2024-12-09 06:10:48

一个有动力的人可能会从应用程序中的 Python 字节码重建可用的源代码,因此您可能会重新考虑对 py2app 的反对。如果您不信任最终用户,为什么要与他们做生意?

A motivated person could probably reconstruct usable source code from the Python bytecode in your app, so you might reconsider your opposition to py2app. If you don't trust your final users, why are you doing business with them?

凉世弥音 2024-12-09 06:10:48

2:您无法“转换”它,但可以将可执行文件移动到 .app 文件中的 App.app/Contents/MacOS/something,并设置 CFBundleExecutable到“某事”。通常不建议这样做。

2: You can't "convert" it, but you can move the executable to App.app/Contents/MacOS/something in a .app file, with CFBundleExecutable set to "something". This would not generally be recommended.

甚是思念 2024-12-09 06:10:48

在为 Windows 用户使用 py2exe 后,他们就不必处​​理库版本了,我已经拆开了编译后的程序,它们包括 python 字节码文件。虽然你可以将查看这些内容视为违反许可证,但事实是,如果计算机可以执行它们,我就可以读取它们。可以通过 C 预处理器用 gcc 编译 python 程序(尝试在 google 上查找 2c.py),我不知道它们是否支持 GCC。同样,使用它们不会获得任何安全性,但可以显着提高速度。

Having used py2exe for windows users so they wouldn't have to deal with library versions, I've torn apart the compiled programs, they include the python bytecode files. While you can make it a violation of the license to look inside those, the fact is that if a computer can execute them, I can read them. It is possible to compile python programs with gcc, via a C preprocessor (try looking for 2c.py on google), I don't know if any of them support GCC. Again, you don't gain any security through using them, but you can get a significant speed improvement.

灯下孤影 2024-12-09 06:10:48

我还没有在大型 Python 项目中尝试过,但对于我自己的脚本,我发现最简单的方法是使用 Automator

您可以使用运行 Shell 脚本操作以交互方式创建应用程序项目,然后将脚本粘贴到其编辑器中,选择您的 shell 程序(/usr/bin/python),最后保存项目。您也拥有了一个 Mac 原生应用程序。

Automator 也可以由 AppleScript 驱动。因此,您可以将此 py-2-app 转换过程通过管道传输到您的构建脚本。

我从未用它测试过 GUI 程序,所以我不知道你是否会满意它。但我会尝试一下,因为您可能想知道所有引用的第 3 方 python 模块/应用程序的维护情况如何,以及它们能持续多久。 Automator 很可能会与 OS X 捆绑在一起,除非苹果真的厌倦了它。

I haven't tried it with big Python projects, but for my own scripts, the easiest way I found was to use Automator

You can interactively create an app project with Run Shell Script action, then paste in your script in its editor, select your shell program (/usr/bin/python), finally save the project. And you have yourself a Mac native app.

Automator can also be driven by AppleScript. So you can pipeline this py-2-app conversion process to your build scripts.

I've never tested a GUI program with it so I don't know if you'll be happy with it. But I'd give it a try since you may wonder how well all the cited 3rd-party python modules/applications are maintained, and how long they are gonna last. Coming bundled with OS X, Automator will likely stay, unless Apple got REALLY tired of it.

离线来电— 2024-12-09 06:10:48

cxFreeze 是可用的最佳解决方案,首先使用 python 创建程序或应用程序,然后进行您的应用程序的 设置 文件,然后使用以下命令构建应用程序构建命令python setup.py build,根据您的要求需要进行一些更改。

cxFreeze is best solution available, first create your program or application using python and than make setup file for your application, and than build the app using build command python setup.py build, according to your requirement you need to make some changes.

痞味浪人 2024-12-09 06:10:48

唯一的方法是py2app。你没有别的办法。对不起。
你所做的研究看起来非常扎实,你没有错过任何东西。

The only way is py2app. You have no other way. Sorry.
The research you did seems very solid and you did not miss anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文