+ 3

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?

5th Mar 2018, 6:58 AM
Sean
Sean - avatar
7 Answers
+ 1
I couldn't get the recommended code to work so I decided on using. def start(): print("example") start() start()
6th Mar 2018, 6:04 AM
Sean
Sean - avatar
+ 10
import os
5th Mar 2018, 9:21 AM
Ahri Fox
Ahri Fox - avatar
+ 3
if your python version is 3, use importlib.reload(yourModuleName) .
5th Mar 2018, 7:18 AM
Sylar
+ 1
It didn't work. Module name is the name of my file right?(test.py). If so the code didn't work, gave me this error: File "C:\Users\Student\Desktop\Test.py", line 188,in <module> importlib.reload(test.py) 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?
5th Mar 2018, 7:36 AM
Sean
Sean - avatar
+ 1
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)
5th Mar 2018, 7:46 AM
Sylar
+ 1
The error has changed to NameError: name 'test' is not defined. What does it mean :/
5th Mar 2018, 8:57 AM
Sean
Sean - avatar
0
You must import your code aka module first. reload function just ..well.. reload specific module without restarting interpreter.
5th Mar 2018, 9:00 AM
Sylar