Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update an executed script import in the Python shell after editing it?

Beginners question. I have started my iPython shell and I am running scripts with the run-command and everything works great. However running a script file and then editing a imported script file and then trying to run it again causes the old imported file to run. So I am guessing python saves some kind of cache for speed. I've tried clear command and such but to no avail.

Basically my problem is this. Two files: function.py and program.py. The program file imports the function. But running the program and then editing the function and then running the program again causes the old function/un-edited version to run.

like image 671
Reed Richards Avatar asked Feb 04 '23 01:02

Reed Richards


2 Answers

Inside of iPython or the standard Python interpreter, you can use the reload() function to reload an imported module.

Example:

In [1]: import foo
  # make some changes to the foo.py
In [2]: reload(foo)
like image 182
Garrett Hyde Avatar answered Feb 05 '23 16:02

Garrett Hyde


For Python 3.4 and above

import importlib
import foo

# Make changes on foo.py

importlib.reload(foo)
like image 22
ABHi Avatar answered Feb 05 '23 14:02

ABHi