We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,337 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Restart your python program.

By Gribouillis on Feb 14th, 2010 5:12 pm

This snippet defines a function restart_program() which restarts your python program from within your python program.

import sys
import os

def restart_program():
    """Restarts the current program.
    Note: this function does not return. Any cleanup action (like
    saving data) must be done before calling this function."""
    python = sys.executable
    os.execl(python, python, * sys.argv)

if __name__ == "__main__":
    answer = raw_input("Do you want to restart this program ? ")
    if answer.strip() in "y Y yes Yes YES".split():
        restart_program()

Cool thanks!
Simple and elegant

Stefano Mtangoo
Senior Poster
3,731 posts since Jun 2007
Reputation Points: 462
Solved Threads: 396
Skill Endorsements: 0

Great...

Boubakr
Newbie Poster
16 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This code is not working on my version of Python (3.2). It just restarts the shell and does not do anything else.

Edwin Okli
Newbie Poster
4 posts since Feb 2012
Reputation Points: 16
Solved Threads: 0
Skill Endorsements: 0

This code is not working on my version of Python (3.2). It just restarts the shell and does not do anything else.

If you are using this in Idle, it won't work because the python process running in the shell is different from Idle gui's process. This will only restart the process running in the shell, not Idle itself.

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

If you are using this in Idle, it won't work because the python process running in the shell is different from Idle gui's process. This will only restart the process running in the shell, not Idle itself.

It is not working in either the shell or Idle. It's strange because when I was running the program on another computer (which I suppose must use a different version of Python) it worked completely fine.

Edwin Okli
Newbie Poster
4 posts since Feb 2012
Reputation Points: 16
Solved Threads: 0
Skill Endorsements: 0

It is not working in either the shell or Idle. It's strange because when I was running the program on another computer (which I suppose must use a different version of Python) it worked completely fine.

What do you exactly mean by 'the shell' ? also what's your OS ?

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

What do you exactly mean by 'the shell' ? also what's your OS ?

i.e. I double-click the .py file to execute it. It works in both the shell and IDLE in the version which I use at home (Python 3.1.2 on Windows Vista) but neither in the version I use at school (Python 3.2 on Windows 7).

Edwin Okli
Newbie Poster
4 posts since Feb 2012
Reputation Points: 16
Solved Threads: 0
Skill Endorsements: 0

Is that 3.2 3.2.2 as there was an issue with input statement with 3.2.1? Probably not reason though, at that problem would make program OK in IDLE and not OK in direct excecution.

pyTony
pyMod
Moderator
6,312 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26

Sorry about this, it turns out it was just my account... which is a bit bizarre but it seems to be working fine for others.

Edwin Okli
Newbie Poster
4 posts since Feb 2012
Reputation Points: 16
Solved Threads: 0
Skill Endorsements: 0

Wow thanks so much!

Aoradon
Newbie Poster
1 post since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0921 seconds using 2.67MB