Can executables made with py2app include other terminal scripts and run them?

隐身守侯 提交于 2019-12-04 01:56:37

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

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.

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.

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.

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)

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

You can always include the os module and call os.system(script);. It executes the given argument in the terminal/command line. For example:

import os
script = " a big long bash script "
os.system(script);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!