f



Using Python To Launch Python

Hello All,
  I have a situation where I can count on a Python installation being
available on a system, but I can't count on it being a version of
Python needed by my application.  Since my application has it's own
version of Python installed with it how should I use the system Python
to launch the version of Python that launches my Application.  Yes,
this is a convoluted process, but not all Pythons are built the
same :)

Right now I am leaning towards using exec to start a new process, but
I thought I would check to see if anyone else has had the need to
perform a task similar to this one.

AHA
0
7/14/2008 9:01:04 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

5 Replies
1037 Views

Similar Articles

[PageSpeed] 39

--vni90+aGYgRvsTuO
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline

On Mon, Jul 14, 2008 at 02:01:04PM -0700, aha wrote:
> Since my application has it's own version of Python installed with
> it how should I use the system Python to launch the version of
> Python that launches my Application.  Yes, this is a convoluted
> process, but not all Pythons are built the same :)

/usr/local/bin/$APPNAME:

#!/bin/sh

INSTALLPATH=<wherever app is installed>
PATH=$INSTALLPATH/bin:$PATH
exec $INSTALLPATH/bin/python $APPNAME "$@"

Doesn't get much simpler than that. :)  You can certainly do the
equivalent in Python... there's not much difference.  Slightly less
typing in bourne/bash shell, I guess...

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D


--vni90+aGYgRvsTuO
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFIe8QmdjdlQoHP510RAsFMAJ0bs2ikCfdLfkgIk/nczw4KH63ZjwCfWiAi
LuLK54OvQcANMNItfeR0aks=
=gq7l
-----END PGP SIGNATURE-----

--vni90+aGYgRvsTuO--
0
code210 (96)
7/14/2008 9:24:54 PM
On Jul 14, 4:01=A0pm, aha <aquil.abdul...@gmail.com> wrote:
> Hello All,
> =A0 I have a situation where I can count on a Python installation being
> available on a system, but I can't count on it being a version of
> Python needed by my application. =A0Since my application has it's own
> version of Python installed with it how should I use the system Python
> to launch the version of Python that launches my Application. =A0Yes,
> this is a convoluted process, but not all Pythons are built the
> same :)
>
> Right now I am leaning towards using exec to start a new process, but
> I thought I would check to see if anyone else has had the need to
> perform a task similar to this one.
>
> AHA

As an alternative, you may be able to use the subprocess module of
Python to do this too.

Mike
0
kyosohma (1259)
7/14/2008 9:32:14 PM
aha wrote:
> Hello All,
>   I have a situation where I can count on a Python installation being
> available on a system, but I can't count on it being a version of
> Python needed by my application.  Since my application has it's own
> version of Python installed with it how should I use the system Python
> to launch the version of Python that launches my Application.  Yes,
> this is a convoluted process, but not all Pythons are built the
> same :)
> 
> Right now I am leaning towards using exec to start a new process, but
> I thought I would check to see if anyone else has had the need to
> perform a task similar to this one.
> 
> AHA

You didn't tell us what operating system, but if by chance it is Windows you 
should use py2exe to package up your program (along with the proper 
pythonXX.dll) into a distributable package.

On Linux, others have posted answers.

-Larry
0
larry.bates (889)
7/14/2008 10:27:50 PM
--ey/N+yb7u/X9mFhi
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline

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)




--ey/N+yb7u/X9mFhi
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFIe/G4HEnASN++rQIRAsZcAKC8HRYRD8ysjmVGNzYVozzeL/27gACdEvLw
7d9Nwv9bzbvSTYEgOSgx3m0=
=4xtx
-----END PGP SIGNATURE-----

--ey/N+yb7u/X9mFhi--
0
code210 (96)
7/15/2008 12:39:20 AM
En Mon, 14 Jul 2008 21:39:20 -0300, Derek Martin <code@pizzashack.org>  
escribi�:
> 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/

Using the standard cmd.exe, the previously posted shell script becomes:

=== begin appname.cmd ===
set INSTALLPATH=<wherever app is installed>
call %INSTALLPATH%\bin\python %INSTALLPATH%\APPNAME.py %*
=== end appname.cmd ===

-- 
Gabriel Genellina

0
gagsl-py2 (3707)
7/15/2008 8:28:12 AM
Reply: