Hi there!

First off, let me say I HAVE Googled the subject, but all provided functions... didn't work. Simply put, I want a function that closes my Python script, and then reopens it. I tried making my own, which would open the same file, sleep 2 seconds, and then close, but to no avail.

Does anyone here have any pointers? Thanks!

Recommended Answers

All 12 Replies

You should explain what you mean by "closing your python script, then reopen it". Do you mean ending the current program execution and start a new execution of the same program ? In that case, you could use functions like os.exec which replaces the current process by another one, or os.spawn which starts a new process (or also subprocess.Popen ), and also sys.exit which exits the current program. So I think you should describe more precisely what you want to do.

I have an inkling that he means reload a module... I too have looked around google and found some sites that insist that it can be done. I just don't know how.

To further clarify when I'm developing a module and I import it into my python shell then realize I need to change the module, instead of closing the shell, modifying the module, then reopening the shell and reimporting and doing all the steps I could just 're'-import it and have it pull in the newer version... if that makes sense.

i second Gribouillis but maybe this will work?

def func(script,numberofruns):
[INDENT]runs = 0
while runs <= numberofruns:
runs = runs + 1
return runs[/INDENT]

function takes script and number of times you want to run it, returns the number of times it actually did.

I want to reload the entire script, not just a module... I'll look in to spawn, though. Thanks!

Also, if this helps, this is the error I get when I try to open the same file, before exiting:

Traceback (most recent call last):
File "C:\Python25\Projects\SUSIE\Susie.py", line 119, in <module>
File "C:\Python25\lib\subprocess.py", line 594, in __init__
errread, errwrite)
File "C:\Python25\lib\subprocess.py", line 816, in _execute_child
WindowsError: [Error 193] %1 is not a valid Win32 application

I'm not on windows, but may be your command should be C:...\python C:...\Susie.py. I mean the python executable first and the script as first argument.

subprocess.Popen("C:\Python25\python.exe C:\Python25\Projects\SUSIE\Susie.py") # 2nd para is name of current file

When this is run, the script doesn't crash, but sys.exit() never executes, either... ideas?

hm. Did you try

subprocess.Popen(["C:\Python25\python.exe", "C:\Python25\Projects\SUSIE\Susie.py"])


Okay, after playing with it a while, I found something: It works in IDLE, but not when the file is loaded by itself... I think the program might be thinking I'm being stupid for trying to open what's already open, so not doing it, but I am not at all sure. Do you have any ideas?

Thanks for all of your help.

Actually, is there a way to just go to "Line 1" of the Python script, thus resetting it? :P That would probably work.

If you want to go to line 1, why don't you just write a loop like this

while True:
  # ... some code ...
  if something:
    continue # restarts on top of the loop
  # ... some code ...


just wanted to point something out.
the difference between idle and just executing a program is that idle doesn't shut off at the end. i don't know if your program takes input but if it only prints things it wil look like it doenst work when you just exec it because it shuts down so fast.
dont know if this has anything to do with your problem but you could solve this by putting
raw_input() at the end of your program wich will force you to press enter before it shuts off

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.