Using Python To Launch Python

Derek Martin code at pizzashack.org
Mon Jul 14 20:39:20 EDT 2008


On Mon, Jul 14, 2008 at 05:40:43PM -0400, Aquil H. Abdullah wrote:
> You've hit the proverbial nail with the hammer.  The problem is that my
> application needs to run under both the Linux and Windows OSs, so while I
> would love to use a nice sh, csh, or bash shell script. My hands are tied
> because Windows does not provide such wonderful shells.

*Provides*, no... neither does it provide Python, for what that's
worth.  But you can certainly get it (bash):

  http://win-bash.sourceforge.net/

I suppose it's not worth installing just for this purpose though...
But you can provide with your application a DoS batch file that does
exactly the same thing (in addition to a shell script).  The user
would quite intuitively use whichever were appropriate, or follow your
provided directions otherwise.  Or, the equivalent in (hopefully
OS-agnostic) Python:

import os, sys

# I believe this gets the name of the root in all major OSes
def root_dir(path):
	if os.path.dirname(path) == path:
		return path
	return (root_dir(os.path.dirname(path)))

appname = <name of your python script>
root = root_dir(os.getcwd())
install_path = os.path.join(root, "usr")
bin_path = os.path.join(install_path, "bin")
os.environ["PATH"] = bin_path + os.pathsep + os.environ["PATH"]
python_path = os.path.join(bin_path, "python")
args = sys.argv[1:]
args.insert(0, os.path.join(bin_path, appname))
args.insert(0, python_path)
args.insert(0, python_path)
os.execv(python_path, args)



-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20080714/e04dcf04/attachment.sig>


More information about the Python-list mailing list