Launching an independent Python program in a cross-platform way (including mac)
André
andre.roberge at gmail.com
Mon Apr 30 20:36:36 EDT 2007
As promised, here's the solution I came up with for launching an
external python script. The function below has been edited (read
simplified slightly) from the original one used in Crunchy. Note that
while the original has been tested, the following has not- but it
should provide a good start *if* problems are found with it.
André
====================
def exec_external(path):
"""execute code saved in file found in 'path' in an external
process
currently works under:
* Windows NT (tested)
* GNOME
* OS X
This also needs to be tested for KDE
and implemented some form of linux fallback (xterm?)
"""
if os.name == 'nt':
current_dir = os.getcwd()
target_dir, fname = os.path.split(path)
os.chdir(target_dir) # change dir so as to deal with paths
that
# include spaces
if console:
Popen(["cmd.exe", ('/c start python %s'%fname)])
else:
Popen(["cmd.exe", ('/c python %s'%fname)])
os.chdir(current_dir)
elif sys.platform == 'darwin':
pth, fn = os.path.split(path)
activate = 'tell application "Terminal" to activate'
script = r"cd '\''/Users/andre/CrunchySVN/branches/
andre'\'';python '\''test.py'\'';exit"
do_script = r'tell application "Terminal" to do script
"%s"'%script
command = "osascript -e '%s';osascript -e '%s'"%(activate,
do_script)
os.popen(command)
elif os.name == 'posix':
try:
os.spawnlp(os.P_NOWAIT, 'gnome-terminal', 'gnome-
terminal',
'-x', 'python', '%s'%path)
except:
try: # untested
os.spawnlp(os.P_NOWAIT, 'konsole', 'konsole',
'-x', 'python', '%s'%path)
except:
raise NotImplementedError
else:
raise NotImplementedError
More information about the Python-list
mailing list