Goal
I would like to import a custom module from the interpreter, run it, modify it, reload it, and run it again to see the changes.
Background
I'm using python 2.7. Before I started writing my own modules, I made great use of the reload()
function in python 2.7.
I reproduced my issue in a simple example
- I create a folder called:
demo
- Inside
demo
, I place two files:__init__.py
andplotme.py
My __init__.py
file contains:
from .plotme import plot
My plotme.py file contains:
import matplotlib.pyplot as plt
import numpy as np
def plot():
x=np.linspace(0,np.pi*4,1000)
y=np.cos(x)
plt.plot(x,y,'b')
I run it with the commands:
>>> import demo
>>> demo.plot()
and it works just fine.
Next, I decide that I want the plot to be red, not blue. I modify 'b'
to 'r'
in plotme.py and save. I then type:
>>> import(demo)
>>> demo.plot()
and the plot is still blue, not red. Not what I want. Instead I try:
>>> reload(demo)
>>> demo.plot()
and again, the color has not updated.
I figure that there needs to be a reload command inside \__init__.py
. I try updating it to:
from .plotme import plot
reload(plot)
and when I type:
>>> reload(demo)
I get the error:
TypeError: reload() argument must be module
I instead try reloading with reload(.plotme)
. Same error. When I try reload(plotme)
, it doesn't throw an error, but the color of the plot isn't updating to red.
How do I fix this?
I would prefer not to have to close and relaunch the interpreter every time I modify a few lines of code.