Re: Launching an independent Python program in a cross-platform way (including mac)



Prateek wrote:
On Apr 30, 4:32 am, André <andre.robe...@xxxxxxxxx> wrote:
I would like to find out how I can launch an independent Python
program from existing one in a cross-platform way. The result I am
after is that a new terminal window should open (for io independent of
the original script).

The following seems to work correctly under Ubuntu and Windows ... but
I haven't been able to find a way to make it work under Mac OS.

def exec_external(code, path):
"""execute code in an external process
currently works under:
* Windows NT (tested)
* GNOME (tested) [January 2nd and 15th change untested]
This also needs to be implemented for OS X, KDE
and some form of linux fallback (xterm?)
"""
if os.name == 'nt':
current_dir = os.getcwd()
target_dir, fname = os.path.split(path)

filename = open(path, 'w')
filename.write(code)
filename.close()

if os.name == 'nt':
os.chdir(target_dir) # change dir so as to deal with paths
that
# include spaces
Popen(["cmd.exe", ('/c start python %s'%fname)])
os.chdir(current_dir)
elif os.name == 'posix':
try:
os.spawnlp(os.P_NOWAIT, 'gnome-terminal', 'gnome-
terminal',
'-x', 'python', '%s'%path)
except:
raise NotImplementedError
else:
raise NotImplementedError
==========================
Any help would be greatly appreciated.

André

Well,

You need to check sys.platform on the Mac instead of os.name.
os.name returns 'posix' on all *nix based systems. sys.platform
helpfully returns "darwin" on the Mac.

Not sure how to start Terminal. Here's what I got when I tried it:

if sys.platform == "darwin": os.spawnlp(os.P_NOWAIT, '/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal')
9460
2007-04-30 05:19:59.255 [9460] No Info.plist file in application bundle or no NSPrincipalClass in the Info.plist file, exiting


There are extension modules on the Mac for integrating Python and AppleScript (the best one is appscript). However, if you want to limit yourself to core Python, your best best is osascript, a system command-tool that lets you call AppleScript code with arguments from other programs. This can be called via os.system.

The basis syntax for doing this from Python might look something like this:

os.system('osascript -e \'tell app \"Terminal\" to activate\'')

This simply launches Terminal. Note that you have to deal with quoting issues. The equivalent syntax from AppleScript would be:

tell app "Terminal" to activate

If you want to Terminal to run a command-line program from AppleScript, you can do this with the "do script" command. Code to do this could look something like this:

myscript = "python -e foo.py"
os.system('osascript -e '\tell app \"Terminal"\ to do script %s\'', myscript)


I haven't tested this, but you get the basic idea--define the script and command-line paramaters in a string, then pass that to AppleScript/osascript as a variable. This code should launch Terminal, then run the external Python script.

HTH,
Kevin

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
.



Relevant Pages

  • Re: Launching an independent Python program in a cross-platform way (including mac)
    ... AppleScript. ... yourself to core Python, your best best is osascript, a system ... If you want to Terminal to run a command-line program from AppleScript, ... you can do this with the "do script" command. ...
    (comp.lang.python)
  • Re: Accessing external file output from script?
    ... > I believe you are saying that you want to know a Python idiom ... > perhaps with a command-line argument or two, ... > result back into a Python variable for subsequent processing. ... call of commands.getstatusoutput not being understandable to Windows. ...
    (comp.lang.python)
  • Re: pygame and python 2.5
    ... I committed to Python because it's a great language. ... modules on the Windows platform without having a copy of Visual Studio ... developing a math library based on GMPY to use ... obsolete compiler that's not even available. ...
    (comp.lang.python)
  • Re: Please test Phatch on Windows (was Re: ANN: Phatch = PHoto bATCH processor and renamer based on
    ... You'll be happy to hear that it appears to work on Vista, though I blush to admit I actually have a Python running on that platform. ... to port the code of Phatch fully to Windows as there were many issues. ... Common - Copies the most common pixel value ...
    (comp.lang.python)
  • ANN: wxPython 2.6.2.1
    ... There have been many enhancements ... wxPython is a GUI toolkit for the Python programming language. ... Currently supported platforms are 32-bit Microsoft Windows, ...
    (comp.lang.python.announce)