Run a python script as an exe and run a new process from it

V

venutaurus539

Hello all,
I've a strange requirement where I need to run a python
script just as we run an exe (by double clicking through windows
explorer or by typing the script name at command prompt). In that
process I should be able to execute another python script in such a
way that, the second script should continue running but the main one
should terminate without effecting the second one.

My requirement may be little confusing so please
get back if you didn't understand what I meant above.


Thank you,
Venu.
 
T

Tim Wintle

Hello all,
I've a strange requirement where I need to run a python
script just as we run an exe (by double clicking through windows
explorer or by typing the script name at command prompt).
I don't know how windows deals with this part
In that
process I should be able to execute another python script in such a
way that, the second script should continue running but the main one
should terminate without effecting the second one.
standard daemon behavior I'd have thought

This shows how you would do it (with lots of comments). Assume this
should work on Windows.

http://code.activestate.com/recipes/278731/

note the two child threads to prevent zombies - may not be needed on
windows but good to use them anyway.
 
V

venutaurus539

I don't know how windows deals with this part>  In that

standard daemon behavior I'd have thought

This shows how you would do it (with lots of comments). Assume this
should work on Windows.

http://code.activestate.com/recipes/278731/

note the two child threads to prevent zombies - may not be needed on
windows but good to use them anyway.

Thanks for the reply,
Being a newbie to python, I am finding it difficult to
understand the logic even after thorough reading of comments. Is there
any simpler way where I can just run a python script from the main
script and exit without disturbing the second one(This will end based
on some other constraints which can be handled). Or can some one throw
some light on where should I run the function os.system(" python
script2.py") from the main one.

Thank you,
venu
 
V

venutaurus539

Thanks for the reply,
           Being a newbie to python, I am finding it difficult to
understand the logic even after thorough reading of comments. Is there
any simpler way where I can just run a python script from the main
script and exit without disturbing the second one(This will end based
on some other constraints which can be handled). Or can some one throw
some light on where should I run the function os.system(" python
script2.py") from the main one.

Thank you,
venu

Adding to the above.. I've to do it in Windows platform and what I can
see from the illustration (/dev/null) etc. they are for Unix
Environment.

Thank you,
Venu
 
T

Tim Wintle

Thanks for the reply,
Being a newbie to python, I am finding it difficult to
understand the logic even after thorough reading of comments. Is there
any simpler way where I can just run a python script from the main
script and exit without disturbing the second one(This will end based
on some other constraints which can be handled). Or can some one throw
some light on where should I run the function os.system(" python
script2.py") from the main one.

That would be a simple way of doing it :)
 
V

venutaurus539

That would be a simple way of doing it :)

When I run that script on Windows it fails throwing the below error:

AttributeError: 'module' object has no attribute 'fork'

Please help me out..
 
G

Gabriel Genellina

En Thu, 26 Feb 2009 13:32:27 -0200, (e-mail address removed)
When I run that script on Windows it fails throwing the below error:

AttributeError: 'module' object has no attribute 'fork'

Please help me out..

Unless you have any special needs, just stick to os.system("...")
 
P

Paddy O'Loughlin

Try this as an outline:
#### script1.py
from subprocess import Popen

if __name__ == '__main__':
scriptname = "script2.py"

Popen("python %s" % scriptname, shell=True)


print "I'm done"


#### script2.py
from time import sleep

if __name__ == '__main__':
while (True):
print "waiting.."
sleep(2)

###########################
If you install python using the Windows Installer (.exe), it should
set up .py files to be opened by python when double-clicking on them.
Alternatively, you can right-click on your .py file and go to "Open
With..." then "choose program", then click "Browse...", you can Browse
to the python executable, so Explorer will use it to open .py files
when you double-click on them.

As someone else mentioned there is also a py2exe program. Google it.
 
V

venutaurus539

Try this as an outline:
#### script1.py
from subprocess import Popen

if __name__ == '__main__':
        scriptname = "script2.py"

        Popen("python %s" % scriptname, shell=True)

        print "I'm done"

#### script2.py
from time import sleep

if __name__ == '__main__':
    while (True):
        print "waiting.."
        sleep(2)

###########################
If you install python using the Windows Installer (.exe), it should
set up .py files to be opened by python when double-clicking on them.
Alternatively, you can right-click on your .py file and go to "Open
With..." then "choose program", then click "Browse...", you can Browse
to the python executable, so Explorer will use it to open .py files
when you double-click on them.

As someone else mentioned there is also a py2exe program. Google it.

2009/2/26 (e-mail address removed) <[email protected]>:

Thanks for the reply,,
I am trying to use the above application using psexec()in
command line.But it failed returning the error message

exited with error code 255.

But when I ran the application normally it worked
fine.Do I need to add anything to my python script(either the main one
or the script which is calling it) to make it work.

Thank you,
Venu Madhav.
 
P

Paddy O'Loughlin

2009/2/27 [email protected] said:
Thanks for the reply,,
           I am trying to use the above application using psexec()in
command line.But it failed returning the error message

 exited with error code 255.

             But when I ran the application normally it worked
fine.Do I need to add anything to my python script(either the main one
or the script which is calling it) to make it work.

I don't have any experience with psexec, so I'm not sure if I can help
you with that.
I assume this means that you are trying to run the script on a remote
computer? (Elsewise, why use psexec?)

I took a look at it though and it seems that it always exits with
*some* error code. I think it's more usual for that error code to be
zero, though (it does when I run it on my machine).

You might try "psexec start script1.py", which might work if you've
successfully set Explorer to run .py files with python.exe when you
double-click on them.

"psexec python script1.py" will work if the python.exe is in the correct path.

You can also specify the full path to your python.exe. if you know it
e.g.: "psexec C:\python25\python.exe script1.py"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top