473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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é

Apr 29 '07 #1
8 2259
On Apr 29, 8:32 pm, André <andre.robe...@gmail.comwrote:
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.
Forgot to add that there was an import as follows:

import os # should have been obvious
from subprocess import Popen # slightly less so

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é

Apr 29 '07 #2
On Apr 30, 4:32 am, André <andre.robe...@gmail.comwrote:
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 bundleor no NSPrincipalClass in the Info.plist file, exiting
Maybe I'm just calling it wrong and you'll have more luck.

Prateek

Apr 29 '07 #3
Prateek wrote:
On Apr 30, 4:32 am, André <andre.robe...@gmail.comwrote:
>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
Apr 30 '07 #4
On Apr 30, 10:59 am, Kevin Walzer <k...@codebykevin.comwrote:
[snip]
>
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,
Thanks. I managed to get something like this to work. Later, I'll
post it as an example to this thread so that other can use if for a
reference if needed.

André
Kevin

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

Apr 30 '07 #5
I would like to see this as a command along with something to open web
pages.. Just one command instead of trying to figure out all the
different op systems. look forward to seeing your code

https://sourceforge.net/projects/dex-tracker
On Apr 30, 9:40 am, André <andre.robe...@gmail.comwrote:
On Apr 30, 10:59 am, Kevin Walzer <k...@codebykevin.comwrote:
[snip]


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,

Thanks. I managed to get something like this to work. Later, I'll
post it as an example to this thread so that other can use if for a
reference if needed.

André
Kevin
--
Kevin Walzer
Code by Kevinhttp://www.codebykevin.com- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Apr 30 '07 #6
On Apr 30, 11:29 am, "Eric_Dex...@msn.com" <Eric_Dex...@msn.com>
wrote:
I would like to see this as a command along with something to open web
pages.. Just one command instead of trying to figure out all the
different op systems. look forward to seeing your code

https://sourceforge.net/projects/dex-tracker

On Apr 30, 9:40 am, André <andre.robe...@gmail.comwrote:
On Apr 30, 10:59 am, Kevin Walzer <k...@codebykevin.comwrote:
[snip]
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 likethis:
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,
Thanks. I managed to get something like this to work. Later, I'll
post it as an example to this thread so that other can use if for a
reference if needed.
André
Kevin
--
Kevin Walzer
Code by Kevinhttp://www.codebykevin.com-Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Opening web pages cross-platform can be done with the "webbrowser"
module: http://docs.python.org/lib/module-webbrowser.html

Mike

Apr 30 '07 #7
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
May 1 '07 #8
My apologies about the last post; I posted my "test" code by mistake,
with hard-coded path information. Here's for future reference
something that is general and should work cross-platform.
André

def exec_external(code=None, path=None):
"""execute code 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 path is None:
path = os.path.join(os.path.expanduser("~"), "temp.py")
if os.name == 'nt' or sys.platform == 'darwin':
current_dir = os.getcwd()
target_dir, fname = os.path.split(path)

if code is not None:
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 sys.platform == 'darwin': # a much more general method can
be found
# in SPE, Stani's Python Editor -
Child.py
activate = 'tell application "Terminal" to activate'
script = r"cd '\''%s'\'';python '\''%s'\'';exit"%(target_dir,
fname)
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
May 1 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: john67 | last post by:
The company I work for is about to embark on developing a commercial application that will cost us tens-of-millions to develop. When all is said and done it will have thousands of business...
2
by: Steve Barker | last post by:
Hi guys, I have written a web service, but I want clients to be able to consume it without having the .NET framework installed on their PC. The web service doesn't return any values; it is just...
7
by: dhussong | last post by:
I have created a Setup and Deployment project in Visual Studio.NET 2003. After my installation has completed running I'd like to launch the EXE that I just installed. I've found how to launch the...
18
by: diffuser78 | last post by:
I have a python code which is running on a huge data set. After starting the program the computer becomes unstable and gets very diffucult to even open konsole to kill that process. What I am...
4
by: vol30w60 | last post by:
Hi folks, I am trying to launch a program with PHP code. I am running Apache on Windows XP SP2. I am using the method noted here: http://us2.php.net/manual/en/function.exec.php#59428 To...
7
by: skip | last post by:
This question was posed to me today. Given a C/C++ program we can clearly embed a Python interpreter in it. Is it possible to fire up multiple interpreters in multiple threads? For example: ...
7
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
I have a WinForm application that, depending on user actions, may spin for a while doing extensive calculations, showing a progress bar in the meantime. I would like to be able to launch a second...
9
by: erikcw | last post by:
Hi, I have a cgi script where users are uploading large files for processing. I want to launch a subprocess to process the file so the user doesn't have to wait for the page to load. What is...
5
by: Stef Mientki | last post by:
hello, I'm not familiar with Linux / Ubuntu, still trying to get my application working under these operating systems. I've a script "file_support.py", now when I'm in Ubuntu, open a command...
114
by: Andy | last post by:
Dear Python dev community, I'm CTO at a small software company that makes music visualization software (you can check us out at www.soundspectrum.com). About two years ago we went with decision...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.