使用py2app制作的可执行文件可以包含其他终端脚本并运行它们吗? [英] Can executables made with py2app include other terminal scripts and run them?

查看:277
本文介绍了使用py2app制作的可执行文件可以包含其他终端脚本并运行它们吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个适用于OS X的不错的python应用程序,效果很好.它运行一个外部终端脚本,我想将其包含在我的python应用程序中.理想情况下,我将能够运行py2app并将此脚本捆绑到可执行文件中,然后能够将其包含并在我的代码的python部分中运行它.这可能吗?

So I have a nice python app for OS X that is working great. It runs an external terminal script and I would like to include that in with my python app. Ideally, I'd be able to run py2app and have this script bundled up with it into the executable and then be able to include it and run it in the python portion of my code. Is this possible?

提前谢谢!

额外我正在使用的脚本已编译.我不能只看里面并粘贴它.

Extra The script that I am using is compiled. I can't just look inside and paste it.

推荐答案

请参见 http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html#option-reference 并查看--resources参数.例如:

See http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html#option-reference and look at the --resources parameter. For example:

python setup.py py2app --resources foo

如果这是一个Shell脚本,那将是一件完全正确的事情.对于二进制可执行文件,它有点黑.首先,p2app的文档明确指出不适合代码!".其次,OS X文档说不要将可执行文件放在Resources目录中.造成这种情况的主要原因是代码签名:默认设置将资源"中的所有内容密封"为主应用程序签名的一部分,但是不应以这种方式密封单独的可执行文件,而应将它们分别签名.

If this were a shell script, that would be a perfectly valid thing to do. For a binary executable, it's a bit more hacky. First, p2app's documentation clearly says "not for code!". Second, the OS X documentation says not to put executables in the Resources directory. The main reason for this is code signing: the default settings "seal" everything in Resources as part of the main app's signature, but separate executables are not supposed to be sealed that way, they're supposed to be signed separately.

但是,话虽如此,它仍然会起作用.除了它可能不会获得+ x权限之外,因此在py2app步骤之后,您必须"chmod + x MyApp.app/Contents/Resources/foo"使其可运行.

However, all that being said, it will still work. Except that it probably won't end up with +x permissions, so after your py2app step, you'll have to "chmod +x MyApp.app/Contents/Resources/foo" to make it runnable.

您还可以使用distutils package_data,data_files和/或MANIFEST东西添加具有任意相对路径的任意文件,这可能是一个更好的解决方案,但更为复杂.

You can also use the distutils package_data, data_files, and/or MANIFEST stuff to add arbitrary files with arbitrary relative paths, which might be a better solution, but it's more complicated.

无论哪种方式,您都应该在脚本中使用与包相关的路径,可以通过PyObjC轻松访问该路径.鉴于您使用的是PowerPC可执行文件,因此可能需要太多的向后兼容性,以至于不能依赖于此,在这种情况下,您可能只需要使用"../Resources/foo",否则,看起来像这样:

Either way, in your script, you should use the bundle-related path, which you can easily access via PyObjC. Given that you're using a PowerPC executable, you may need so much backward compatibility that you can't rely on that, in which case you may have to make do with just "../Resources/foo", but otherwise, it looks like this:

import Foundation
# ...
bundle = Foundation.NSBundle.mainBundle()
path = bundle.pathForResource_ofType_('foo', None)

然后可以使用NSTask或子进程,Popen,操作系统等启动它.

You can then launch it with NSTask, or subprocess,Popen, os.system, etc.

这篇关于使用py2app制作的可执行文件可以包含其他终端脚本并运行它们吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆