Code to restart script within the Shell. | Sololearn: Learn to code for FREE!


Code to restart script within the Shell.

I want to be able to restart my script within the python shell. The current code I'm using gives me the error that "os" is not defined. Any suggestions?

3/5/2018 6:58:49 AM


7 Answers

New Answer


I couldn't get the recommended code to work so I decided on using. def start(): print("example") start() start()


import os


if your python version is 3, use importlib.reload(yourModuleName) .


It didn't work. Module name is the name of my file right?( If so the code didn't work, gave me this error: File "C:\Users\Student\Desktop\", line 188,in <module> importlib.reload( NameError: name 'importlib' is not defined. Apologies if I fail to understand some things, I'm quite new to this. Should I be using the directory path in the module?


Just import 'importlib' module first because it is a module like your code, right? Then You can use reload function. Like this: from importlib import reload then, reload(test)


The error has changed to NameError: name 'test' is not defined. What does it mean :/


You must import your code aka module first. reload function just ..well.. reload specific module without restarting interpreter.