In a py2app/Mac Application Bundle, is there a way to spawn another instance of same app from within the app, by passing different command line arguments?
or given a mac app bundle, how can I run it from command line and pass some arguments too?
Edit1:forking is a limited option, which may not work with 3rd party executables bundle with app+I need to run this on mac and windows.
Edit2: Question is how to run a a bundled python script using subprocess module
Details:
I am using py2app to generate a app bundle for my appilcation. My application has two parts
- MainApp: which is the UI
- BackgroundApp: a background process, which does the real job
Both MainApp and BackgroundApp have been implemented as python script and actually they are the same python script with different commandline e.g.
python myapp.py
python myapp.py --backgroundprocess
So when I run python myapp.py it automatically starts background process based on program path, but as I have now bundled my app as py2app I am not sure what executable I should be calling and passing --backgroundprocess option?
What I have tried
$ open MyApp.app/this opens the app but I can't pass the arguments to it, as they will be arguments for open command and will not be passed to my app$ MyApp.app/Contents/MacOS/MyApp --backgroundprocessopens the app but not the backgroun process as it seems arguments are not being passed to app
also it throws error
Traceback (most recent call last):
File "/Users/agyey/projects/myapp/release4.26/py2exe/dist/MyApp.app/Contents/Resources/run.py", line 4, in <module>
from renderprocess import RenderEngineApp
File "renderprocess/RenderEngineApp.pyc", line 6, in <module>
File "wx/__init__.pyc", line 45, in <module>
File "wx/_core.pyc", line 4, in <module>
File "wx/_core_.pyc", line 18, in <module>
File "wx/_core_.pyc", line 11, in __load
ImportError: dlopen(/Users/agyey/projects/myapp/release4.26/py2exe/dist/MyApp.app/Contents/Resources/lib/python2.5/lib-dynload/wx/_core_.so, 2): Library not loaded: @executable_path/../Frameworks/libwx_macud-2.8.0.dylib
Referenced from: /Users/agyey/projects/myapp/release4.26/py2exe/dist/MyApp.app/Contents/Resources/lib/python2.5/lib-dynload/wx/_core_.so
Reason: Incompatible library version: _core_.so requires version 7.0.0 or later, but libwx_macud-2.8.0.dylib provides version 2.6.0
Conclusion: it looks like it may not be possible Launch an app on OS X with command line
open doesn't except arguments.
3 Answers 3
Howto find cwd and execute an arbitrary supplied binary First place the binary in AppName.app/Contents/Resources then run this code from the python script:
import subprocess
process=subprocess.Popen((os.getcwd() + "/3rd_party_binary","--subprocess"))
process.poll() # is running?
Howto properly spawn two version of your python app
Fork is the old tried way to do this on MacOSX (unix)
#!/usr/bin/env python
import os, sys
pid = os.fork()
if pid:
# we are the parent
background_process.start()
os.waitpid(pid, 0) # make sure the child process gets cleaned up
else:
# we are the child
gui_app.start()
sys.exit(0)
print "parent: got it; text =", txt
Multiprocessing in Python is apparently something that works on windows as well which I guess would be interesting to you(?).
4 Comments
Create one application then just call the --bakgroundproccess for the app, then create a shortcut for the backgroundprocess flag. Or two different app files. Ether is a good way
1 Comment
I couldn't get py2app working, but the applications' executable are located in AppName.app/Contents/MacOS/AppName. You can check in AppName.app/Contents/Info.plist file for key <key>CFBundleExecutable</key>, it points to the bundles executable in AppName.app/Contents/MacOS folder.
So you can call it as AppName.app/Contents/MacOS/AppName --backgroundprocess.
If you want the BackgroundApp to run the background process without any arguments, there are two options: a) make the bundle from python script, which runs the background process without any arguments; b) change <key>CFBundleExecutable</key> in Info.plist file to say BackGroundApp_shell, and make a shell script Contents/MacOS/BackgroundApp_shell of the following content:
#!/bin/sh
`dirname "0ドル"`/BackgroundApp --backgroundprocess
assuming the executable is BackgroundApp.
2 Comments
AppName.app/Contents/MacOS/AppName --backgroundprocess but it fails, looks like py2app or mac sets some environment variables before executing the appimport sys; print sys.argv to see how the exec created py2app treats command-line arguments. I don't think this is something concerning env vars. Just a guess: py2app creates an exec which doesn't take arguments, but you can try that simple script, to make sure.