title: ipython: re-importing modules when using %run
body: <p>I love ipython, but I've discovered a problem with %run: imported modules are not reloaded when %run is called repeatedly.</p>
  
  <p>Suppose file ex1.py contains the lines:</p>
  
  <pre><code>import ex2
  ex2.x.append(1)
  print ex2.x
  </code></pre>
  
  <p>And file ex2.py contains:</p>
  
  <pre><code>x = []
  </code></pre>
  
  <p>Now, running python ex1.py from the command line repeatedly prints [1] every time. But invoking <code>%run ex1.py</code> from within ipython repeatedly prints [1], [1,1], [1,1,1], etc. This is because module ex2.py is not reloaded. So we have a problem: the ipython run-script protocol is not reflecting "real world" behavior.</p>
  
  <p>Note:</p>
  
  <pre><code>%load_ext autoreload
  %autoreload 2
  </code></pre>
  
  <p>does <em>not</em> help. Those lines will get ex2.py reloaded only if a change has been made to the ex2.py file. If we don't make any changes, or only make changes to ex1.py, we get the undesired behavior.</p>
  
  <p>Is there any way to make %run behave like the command line here? This seems like a real deficiency with using ipython as a testing environment for scripts. (Or perhaps the moral is that a module shouldn't be writing into another module's namespace?)</p>