1

I ran my script from Python environment launched in bash:

>>> import myscript

I then modified my script a little and save it. Then run again

>>> import myscript

But it doesn't run the updated script.

How can I tell Python to run the updated one? Thanks!

J0HN
  • 26,063
  • 5
  • 54
  • 85
Tim
  • 1
  • 141
  • 372
  • 590
  • Also http://stackoverflow.com/questions/5516783/how-to-reload-python-module-imported-using-from-module-import – devnull Apr 27 '14 at 17:12

2 Answers2

4

Simply reload it like this

reload(myscript)

Quoting from the docs,

Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • so the syntax for import is `import myscript` not `import(myscript)`, while for reload is `reload(myscript)` not `reload myscript`? – Tim Apr 27 '14 at 17:15
  • @Tim `reload` is actually a function, so you need to use it like that only, whereas `import` is a keyword in the language. – thefourtheye Apr 27 '14 at 17:17
  • Thanks. Is import not a function? What is it? (don't say it is a keyword. the name of a build in function is a keyword too) – Tim Apr 27 '14 at 17:21
  • @Tim Keyword is different from a builtin function. The line which has `import` will be normally called as `import` statement. You cannot use that as a variable. – thefourtheye Apr 27 '14 at 17:23
2

reload builtin is what you actually need: https://docs.python.org/2/library/functions.html#reload

Ivan Klass
  • 6,407
  • 3
  • 30
  • 28