9

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.

1
  • Why aren't you running the program.py from the command line? Commented Nov 2, 2010 at 10:02

4 Answers 4

9

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)
Sign up to request clarification or add additional context in comments.

1 Comment

So adding a reload after the import command in the script file actually does the trick! Would be good with some kind of flush command though.
3

For Python 3.4 and above

import importlib
import foo

# Make changes on foo.py

importlib.reload(foo)

Comments

1

Use Python's reload function to recompile the module code and reexecute all module-level code - otherwise the Python interpreter will use the stale compiled bytecode (.pyc). For example:

In [1]: import script

In [2]: run script
...

In [3]: reload(script)
Out[3]: <module 'script' from 'script.pyc'>
...
In [4]: run script

Comments

0

It's stated here that the file gets re-read from disk every time, are you sure the error is not on your side?

1 Comment

Yes my fault. The script file I run gets updated but not the imported script file in the in the script file I am running.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.