0

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 and plotme.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.

martineau
  • 119,623
  • 25
  • 170
  • 301
john
  • 41
  • 1
  • 7
  • but my question is why you aren't adding the color as a parameter to the `plot()` function? So that you can pass there whatever you want? – Moinuddin Quadri Dec 30 '17 at 21:56
  • 1
    and for your issue, you need to again import the module after `reload(...)`. Read [python refresh/reload](https://stackoverflow.com/q/1517038/2063361) – Moinuddin Quadri Dec 30 '17 at 21:58
  • For this simple example, I could do that. But what if I decide to make the 'plot()' function write text instead of plotting, something very fundamentally different. – john Dec 30 '17 at 21:58
  • @MoinuddinQuadri I'm not 100% following. Would you please provide the exact code for me to try? Thanks. – john Dec 30 '17 at 22:00
  • Even in that case, how are you planning to write the text? By updating the source code file each time? I am not sure why you wanna do this, and why can't pass text as param to function. Anyway, I have shared the link above which help you in what you are trying to achieve (again, I strongly suggest that this is not what you should do :-) ) – Moinuddin Quadri Dec 30 '17 at 22:03
  • @MoinuddinQuadri I'm doing a fair amount of data analysis, and I'm constantly tweaking my code to do different things. Some portions of the code will remain fairly static while others may by the minute. This is not traditional programming in the sense that it needs to change regularly. – john Dec 30 '17 at 22:08
  • 1
    @MoinuddinQuadri I changed `\__init__.py` to read `from .plotme import plot` `reload(plotme)` `from .plotme import plot` and it seems to be working. Thanks! While it certainly works, is there a better way to do this? (Having trouble adding code to the comments...) – john Dec 30 '17 at 22:10

3 Answers3

1

At @MoinuddinQuadri 's suggestion, I updated my __init__.py file to contain:

from .plotme import plot
reload(plot)
from .plotme import plot

and it works. It's a tad cumbersome as my end application will have a LOT of independent functions to load, but it works. Thanks, MoinuddinQuadri.

Does anyone have other suggestions? If I have 20 functions, it's a bit tedious to write this out 20 times. Is there a way to reload all functions quickly?

john
  • 41
  • 1
  • 7
1

You must reload both demo module (i.e. the file demo/__init__.py) **and** the filedemo/plotme.pywhich isdemo.plotme. More precisely, as you importplotfunction from theplotmesub module, you must import firstplotmeand thendemo`:

reload(demo.plotme)
reload(demo)

After those 2 commands, any changes in plotme will be taken into account.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Another good solution, thanks! I think this is certainly closer to what I'm looking for, but it still seems cumbersome if I have 20 files that I'm routinely modifying. – john Dec 30 '17 at 23:17
-1

related:

How do I unload (reload) a Python module?
Why are there dummy modules in sys.modules?

my answer:

import sys
for n in filter(lambda x: x.startswith('demo') and x != 'demo', sys.modules):
    del(sys.modules[n])
reload(demo)
Ju Xuan
  • 76
  • 3